java Android Start Activity 无需创建新实例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15904061/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 21:14:42  来源:igfitidea点击:

Android Start activity without creating new instance

javaandroidandroid-layoutandroid-intentandroid-manifest

提问by Ibrahem Ahmed

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

假设我有活动 A 作为我的应用程序的根活动。并形成这个活动我去活动B。

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

我希望能够在不创建活动 A 的新实例的情况下从 B 返回到 A。

this code is in Activity B

此代码在活动 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

但它会在活动 A 上调用 onCreate 。我想要做的是在活动 b 开始时让 A 在后台,每当它完成时切换回活动 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>

回答by Kaidul

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

根据android活动生命周期,当你启动一个活动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 :

当您现在启动活动 B 时:

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 :

当你现在再次启动 A 时:

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 : enter image description here

这是一个活动的生命周期: 在此处输入图片说明

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.

根据默认行为,活动 A 进入 onStop() 状态并且不处于活动状态,需要在返回活动 A 时创建一个新实例。我所知道的 - 无法保持 A 实例的活动状态。

回答by Vu Hong

intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);

Try it... It makes old activity to front without new instance...

试试看......它使旧活动在没有新实例的情况下出现......

回答by Sankar V

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

你不需要做任何事情。只需onBackPressed()从活动 B 中删除您的方法。

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

默认情况下,当您从Activity A移动到Activity B 时,Android 会将Activity A添加到后台堆栈。当您按下Activity B的后退按钮或完成它时,它会自动从 backstack 恢复 Activity A。

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

如果您希望以编程方式完成 Activity B,请finish()在需要时调用 Activity 的方法。

回答by Nirav Ranpara

Use moveTaskToBack(true);

使用 moveTaskToBack(true);

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

or You can also use finish()

或者你也可以使用finish()

回答by jimpanzer

Try this

试试这个

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

And thats all, what you need.

这就是你所需要的。