简体   繁体   中英

Leave activity when back button pressed in fragment

I have an app with multiple activities, each of which may employ multiple fragments.

One activity (called IBAsTabsActivity) extends SherlockFragmentActivity and I am using some example code where a different fragment gets loaded up depending on which tab is selected. One of the possible fragments is called "SearchableListFragment". This is all working fine. I have a "home" activity with two buttons, one to take me to IBAsTabsActivity and another to take me to AnAlternativeActivity. If I press the button to take me to IBAsTabsActivity and look at several tabs - then if I press the back button I jump straight to "home".

Now I want to get AnAlternativeActivity up and running. It has no tabs and so does not employ any Sherlock code. Instead I have this:

public class AnAlternativeActivity extends FragmentActivity implements OnClickListener
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.blank_fragment_holder);

        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        SearchableListFragment fb = new SearchableListFragment();
        ft.replace(R.id.fragment_holding_layout, fb);
        ft.addToBackStack("replacingFragmentA");
        ft.commit();
}

This all works fine in as much as if I click on the button on the home screen to call AnAlternativeActivity, then I see the SearchableListFragment() (inside fragment_holding_layout) and its contents correctly. But now if I click the back button the fragment within fragment_holding_layout disappears, but leaves AnAlternativeActivity still running. I have to press the back button again in order to exit AnAlternativeActivity and get back to my home activity.

I could probably cook up some ugly code to resolve this issue - but I suspect that there is a standard way to cope with this scenario. Any ideas?

You should be able to use the notion of parent-activity to resolve this easily. All you need to do is to tell Android that HomeActivity is the parent of your AnAlternativeActivity and the navigation will be handled by you. In your AndroidManifest.xml file add android:parentActivityName attribute for AnAlternativeActivity like so:

<activity
    android:name="com.mick.AnAlternativeActivity"
    android:label="@string/activity_title"
    android:parentActivityName="com.mick.HomeActivity" >
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value="com.example.mick.HomeActivity" />
</activity>

You need the regular attribute for new devices and meta-data tag for older devices. More information can be found here: http://developer.android.com/training/basics/firstapp/starting-activity.html (look under Add it to the manifest subheading).

Within AnAlternativeActivity's onPause you could call finish() to end it.

public void onPause() {
    super.onPause();
    finish();
}

Your call to ft.addToBackStack("replacingFragmentA"); seems to be the cause for the need for tapping 'Back' twice. Adding the transaction to the back stack will have that effect: adding a layer in the history stack.

So when you press 'Back' the first time that transaction will be 'undone' so to speak, and will replace your SearchableListFragment with the R.id.fragment_holding_layout . The second time you press 'Back' will move from the AnAlternativeActivity back to your 'home' activity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM