Android 如何避免同一活动的多个实例?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10614565/
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-08-20 04:23:25  来源:igfitidea点击:

How to avoid multiple instances of same Activity?

androidandroid-activity

提问by Debojit


In my PoC, I have some Activities, HomeActivity, CreateActivity, SearchActivity, ResultsActivity, ResultDetailsActivity, and UpdateActivity.


在我的 PoC 中,我有一些活动HomeActivityCreateActivitySearchActivityResultsActivityResultDetailsActivity、 和UpdateActivity

I have two main navigation paths: Createand Search.

我有两个主要的导航路径:CreateSearch

Navigation for the Createpath is as follows: HomeActivity--> CreateActivity-(on complete)-> HomeActivity

创建路径的导航如下:HomeActivity--> CreateActivity-(完成时)->HomeActivity

Navigation for Searchis as follows: HomeActivity--> SearchActivity--> ResultsActivity(ListActivity) --> ResultDetailsActivity--> UpdateActivity-(on complete)-> ResultDetailsActivity(with updated data).

搜索导航如下:HomeActivity--> SearchActivity--> ResultsActivity( ListActivity) --> ResultDetailsActivity--> UpdateActivity-(完成时)-> ResultDetailsActivity(更新数据)。

Currently, navigation to a new Activityis via startActivity(intent)method. However, this is causing multiple instances of each Activityto be opened.

目前,导航到新ActivitystartActivity(intent)方法是通过方法。但是,这会导致Activity打开每个实例的多个实例。

I'm rather new to Android. Could someone please suggest how I could avoid this?

我对 Android 比较陌生。有人可以建议我如何避免这种情况吗?

回答by Sherif elKhatib

In your android manifest, Add to your <activity>tag the android:launchMode="singleTask"

在您的 android 清单中,添加到您的<activity>标签android:launchMode="singleTask"

For a full list, check the documentation of activity

如需完整列表,请查看活动文档

In your manifest:

在您的清单中:

    <activity android:name=".YourActivity"
              android:launchMode="singleTask"
              android:label="@string/app_name" />

Note: don't use singleton.

注意:不要使用单例。

回答by waqaslam

Setting either the following flags may help you to resolve your issue:

设置以下任一标志可能会帮助您解决问题:

  • Intent.FLAG_ACTIVITY_CLEAR_TOP
  • Intent.FLAG_ACTIVITY_REORDER_TO_FRONT
  • Intent.FLAG_ACTIVITY_CLEAR_TOP
  • Intent.FLAG_ACTIVITY_REORDER_TO_FRONT

回答by Joonsung Kim

Use Intentflag when startActivity:

在以下情况下使用Intent标志startActivity

Intent intent = new Intent(this, MyActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);

there are many kinds of flag.

国旗有很多种。

This will be helpful: android singleton activity

这会有所帮助: android singleton activity

回答by Aracem

The best form to manage the Activities is use

管理活动的最佳形式是使用

startActivityForRestult(Intent,ID)

startActivityForRestult(Intent,ID)

With this method to call Activities your HomeActivity can manage a result for the other activities in the Override method

使用此方法调用活动,您的 HomeActivity 可以管理 Override 方法中其他活动的结果

    @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
}

You can send known result for the diferents Activities and manage it. For example:
(Allways with startActivityForResult)

您可以发送不同活动的已知结果并对其进行管理。例如:(
始终与 startActivityForResult)

HomeActivity --> SearchActivity --> ResultsActivity(ListActivity) --> ResultDetailsActivity --> UpdateActivity -(on complete)-> ResultDetailsActivity (with updated data). Press Return and send SEARCH_fINISHED -->UpdateActivity catch this and send the same result in the onActivityResult method and finish() --> The same with searchActivity --> Home

HomeActivity --> SearchActivity --> ResultsActivity(ListActivity) --> ResultDetailsActivity --> UpdateActivity -(完成时)-> ResultDetailsActivity(更新数据)。按 Return 并发送 SEARCH_fINISHED -->UpdateActivity 捕获此并在 onActivityResult 方法和 Finish() 中发送相同的结果 --> 与 searchActivity 相同 --> Home

This can help you too:

这也可以帮助您:

Intent.FLAG_ACTIVITY_CLEAR_TOP
Intent.FLAG_ACTIVITY_REORDER_TO_FRONT

Intent.FLAG_ACTIVITY_CLEAR_TOP
Intent.FLAG_ACTIVITY_REORDER_TO_FRONT

http://blog.akquinet.de/2010/04/15/android-activites-and-tasks-series-intent-flags/

http://blog.akquinet.de/2010/04/15/android-activites-and-tasks-series-intent-flags/

回答by Kugan Kumar

Few things to add on to the answer posted by JoonSung Kim, change the addFlag method which will throw "cannot resolve error"

对 JoonSung Kim 发布的答案添加的内容很少,更改将抛出“无法解决错误”的 addFlag 方法

change: intent.addFlag(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);

改变: intent.addFlag(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);

to: intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);

到: intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);

code should be:

代码应该是:

Intent intent = new Intent(this, MyActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);

To prevent multiple instance of a same Activity change the flag

为了防止同一活动的多个实例更改标志

from : intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);

从 : intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);

to: intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

到: intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

code should be:

代码应该是:

Intent intent = new Intent(this, MyActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

回答by Salman Khalid

My problem was setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);in onCreate(...) method of Activity.

我的问题出setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);在 Activity 的 onCreate(...) 方法中。