30


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
  • You can try SingleTon Pattern for it. Commented May 16, 2012 at 8:32

6 Answers 6

41

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I've seen a blog for android:launchMode="singleTop", but unfortunately couldn't understand it very well. May I have a slightly more newbie-friendly source, please?
I was having issues with singleInstance as it affected navigation when going back from an activity opened on top (the prev activity was being killed when going to background). Fortunately singlesTask doesn't have that issue.
36

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

  • Intent.FLAG_ACTIVITY_CLEAR_TOP
  • Intent.FLAG_ACTIVITY_REORDER_TO_FRONT

2 Comments

If I go with Intent flags, then, in my Create path, after creating a new dataset in CreateActivity, should I add Intent.FLAG_ACTIVITY_REORDER_TO_FRONT flag to the code in my button to navigate to the HomeActivity? I've also overriden onBackPressed in CreateActivity with this.finish().
I guess you should Intent.FLAG_ACTIVITY_CLEAR_TOP because then it will clear all the front activities including CreateActivity and take you to first activity which should be HomeActivity
9

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

1 Comment

if you want to prevent multiple instance in same Activity, then FLAG_ACTIVITY_CLEAR_TOP will be nice
3

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/

Comments

1

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

Comments

1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.