How to avoid multiple instances of same Activity?

2020-02-08 05:54发布

问题:


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

I have two main navigation paths: Create and Search.

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

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

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

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

回答1:

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

  • Intent.FLAG_ACTIVITY_CLEAR_TOP
  • Intent.FLAG_ACTIVITY_REORDER_TO_FRONT


回答2:

In your android manifest, Add to your <activity> tag the 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.



回答3:

Use Intent flag when 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



回答4:

The best form to manage the Activities is use

startActivityForRestult(Intent,ID)

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

    @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)

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

This can help you too:

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/



回答5:

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

change: intent.addFlag(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);

to: 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);

to: 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);


回答6:

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