Android going back to old activity

2019-06-11 12:47发布

问题:

I have a Tab A, B and C. All ofthem contain a ListView called Aa, Bb and Cc. When I click on an item in one of these ListViews I want to show a new activity which will display the information of that clicked item. I have enabled the button in the ActionBar which takes me to the parent Activity. However always when I click that button it does take me to the parent activity but a whole different tab is selected and it loads everything again (data for the listviews). Basically it's the same as when I would start the application.

What I want is to go back to the last "state" of my parent activity. I overrode the onNavigateUp() method.

@Override
public boolean onNavigateUp() {
    onBackPressed();
    return false;
}

This works. It goes back to the parent activity and shows it in the state I left it, that is, the tab I was in is still selected and the ListView shows me the items I last saw.

However, now I want to show a notification when I clicked a button in my display Activity. And when I click that notification it should take me to the display Activity.

So far so good but when I click that button so that the Notification shows up and I click on that notification, it takes me again to the display Activity. But the problem is, it is already open and when I click on the navigateUpButton it takes me to the last activity I was showing before so it's the display Activity again. I would have to click it again to show the parent activity.

I hope my problem is clear. How can I show the parent activity in its last showed state when I click that navigateUpButton?

Edit:

I saw in a similar question an approach to add this to my activity in the manifest file :

android:alwaysRetainTaskState="True"
android:launchMode="singleInstance"

It worked, I can now only use one instance. When I click it and I am still in my display Activity it takes me back to my parent Activity. However, when I am in my home screen and click that notification, the display Activity shows up and then with the navigateUpButton I get back to my home screen and not back to my parent Activity.

回答1:

Keep this in your manifest file:

<activity
    android:label="@string/app_name" android:name="ChildActivity"
    android:launchMode="singleTop" >

And

in your onNavigateUp

@Override
public boolean onNavigateUp() {
    Intent intentMain = new Intent(ChildAct.this, MainActivity.class);
    startActivity(intentMain );
    return false;
}

Now a new instance of a "singleTop" activity will only be created to handle a new intent if there is no any instance already. However, if the target task already has an existing instance of the activity at the top of its stack, that instance will receive the new intent (in an onNewIntent() call); a new instance is not created

Refer here

Hope this helps.



回答2:

Can you try this one instead of onNavigateUp() ?

 @Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
    switch (menuItem.getItemId()) {
    case android.R.id.home:
        super.onBackPressed();
        return true;
    }
    return (super.onOptionsItemSelected(menuItem));
}

R.id.home is android defined, not a custom button i added.