开发者

Android activities navigation flow problem

开发者 https://www.devze.com 2023-03-31 13:43 出处:网络
I have activity A that starts activity B(singleInstance). In activity B there is a back button, which starts intent to activity A.

I have activity A that starts activity B(singleInstance). In activity B there is a back button, which starts intent to activity A.

A -> B -> A

I want to achieve following behaviour:

User triggers B on A, after that press my back button, returns to activity A and press devices's hardware back button, which navigates him to device's home screen.

Now it looks like that: User triggers B on A, after that press my back button, returns to activity A and press devices's hardware back button, which navigates him again to activity A, then press back button again and open activity B. Then he presses back button and activity A occurs and it is an endless loop....

I can not use flag NO_HISTORY, because I want to save state of activity B from some other reasons. Activity B is a webBrowser so I have overloaded methods onSaveInstanceState and onRestoreInstanceState to save in cache website which was loaded.

Right now I overloaded onBackPressed method in activity A , by starting an intent to home screen, but it is not a good solution.

Do you have any other ideas?

Here is the code:

Activity A: Intent browser = new Intent(this, B.class); this.startActivity(browser);

Activity B: ` @Override public void onSaveInstanceState(Bundle savedInstanceState) { super.onSaveInstanceState(savedInstanceState);

callWebViewMethod("saveState", new Class[] { Bundle.class }, new Object[] { savedInstanceState }); } @Override public void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState);

    callWebViewMethod("restoreState", new Class[] { Bundle.开发者_运维问答class }, 
            new Object[] { savedInstanceState });
}   
@Override
public void onBackPressed() {
    if (webView != null) {
        if ( ( Boolean )callWebViewMethod( "canGoBack" ) )
        {
            callWebViewMethod("goBack");
        }
        else
        {
            startActivity( getBackIntent() );
        }
    }
}
private Intent getBackIntent() {
    Intent i = new Intent();
    // some code here           
    i.setClass(this, A.class);
    return i;
}`


after that press my back button

finish() activity B here..

remove onBackPressedmethod from your activity A.


If you don't want to navigate back to Activity, you can try to put finish(); after your startActivity();


In your code:

@Override
    public void onBackPressed() {
    if (webView != null) {
        if ( ( Boolean )callWebViewMethod( "canGoBack" ) )
        {
            callWebViewMethod("goBack");
        }
        else
        {
            startActivity( getBackIntent() );
        }
    }
}

You don't need to create a new Intent to go back to Activity A. Just get rid of the Else condition.

    @Override
    public void onBackPressed() {
        if (webView != null) {
            if ( ( Boolean )callWebViewMethod( "canGoBack" ) )
            {
                callWebViewMethod("goBack");
            }
        }
        else {
            super.onBackPressed(); // pass to the default handler
        }
    }


Don't override onBackPressed, don't call finish. When you start each activity, set the intent flags to

Intent.FLAG_ACTIVITY_REORDER_TO_FRONT


While jumping from A>B A to B create the new intent

Intent intent = new Intent(A.this,B.class);
startActivity(intent);

When you press back from the activity B just call the onBackPressed method

onBackPressed();

when you fall back to activity A and u want to jump to HomeActivity from A override the onBackPressed() method.

@Override
    public void onBackPressed() {
       Intent intent = new Intent(A.this,HomeActivity.class);
       startActivity(intent);
    }

If you are jumping from B to home and them press back it will jump back to the last opened activity because android maintain the stack of your activity. Make sure finish() the activity if you don't want the activity in your activity stack or you can clear the whole stack by using this code.

 Intent intent = new Intent(B.this, HomeActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);

This will create the new Activity and their wont be any stack.

0

精彩评论

暂无评论...
验证码 换一张
取 消