简体   繁体   中英

Android Start activity without creating new instance

Assume i have activity A that act as the root activity for my app . and form this activity i go to activity B.

I want to be able to go back from B to A Without creating new instance of Activity A.

this code is in Activity B

public void onBackPressed() {
        super.onBackPressed();
//      Intent intent= new Intent(getBaseContext(), MainActivity.class);
//      intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        Intent myIntent = new Intent(getBaseContext(), MainActivity.class);
        myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(myIntent);
        Log.d("Back", "TEST");
    }

but it sill call the onCreate on activity A . What i want to do is have A in the background when activity b is started and whenever it is finished switch back to activity A

this is the manifest

<activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main"
            android:screenOrientation="unspecified" 
            android:launchMode="singleTask"
            android:stateNotNeeded="false">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                 <category android:name="android.intent.category.HOME"/>
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:label="@string/app_name"
            android:name=".SubmenuActivty" >
        </activity>

According to android activity life cycle, when you launch an activity A :

Life Cycle method list :

Activity A.onResume();
Activity A.onStart();
Activity A.onCreate();

Activity Status :

Activity A : Resumed

When you launch the activity B now :

Life Cycle method list :

Activity A.onStop();  
Activity B.onResume();
Activity B.onStart();
Activity B.onCreate();
Activity A.onPause();
    ...
    ...
    ...

Activity Status :

   Activity A : Stopped
   Activity B : Resumed

And when you again start A now :

Life Cycle method list :

Activity B.onDestroy();  
Activity B.onStop(); 
Activity A.onResume();
  ....
  ....
  ....      

Activity Status :

   Activity B : Destroyed
   Activity A : Resumed

This is the life cycle of an activity : 在此输入图像描述

You can find the details here

According to default behavior, activity A goes to onStop() state and doesn't alive and need to create a new instance on coming back to activity A. All I know - there is no way to keep alive of the A instance.

intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);

尝试一下......它在没有新实例的情况下使旧活动成为现实......

You Dont need do anything. Just remove your onBackPressed() method from Activity B.

By default when you move from Activity A to Activity B , Android adds the Activity A to the backstack. When you press the back button from Activity B or finish it, it automatically restore the Activity A from backstack.

If you wish to finish your Activity B programmatically call activity's finish() method when you want to do it.

Try this

public void onBackPressed() {
    super.onBackPressed();
    Log.d("Back", "TEST");
}

And thats all, what you need.

Use moveTaskToBack(true);

public void onBackPressed() {
// TODO Auto-generated method stub
moveTaskToBack(true); 
super.onBackPressed();
}

or You can also use finish()

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