Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to have one and only one instance of each activity?

I'm finding that in my application, the user can get quite 'nested' in the various activities that are opened while the user is using the application.

For example:

  1. Main Menu
  2. Object List
  3. Object Details
  4. Object Edit
  5. Object Details
  6. Object Child Details
  7. Object Child Edit
  8. Object Child Details

Now, when the user presses back, it has to go through 'Object Child Details' twice (same object, when it is edited it returns to the detailed page), and the same thing happens for the 'Parent Object Details'.

Is there a way to reuse activities, if they are already open in the stack, and reorder them to the front? The only way I have seen is on activities with the launcher attribute. I believe I saw singleTask and singleTop.

If am supposed to be using these two attributes, singleTask and singleTop, how am I supposed to use them? When I tried to include them in the application, it made no difference. Do I also need to set a flag while launching the intent using startActivity?

like image 296
Cody Avatar asked Jan 24 '12 23:01

Cody


People also ask

How do you create an instance of an activity?

Activity instances are always created by the Android system. This is because a lot of initializations have to be done for the activity to work. To create a new activity you call startActivity with an Intent describing the activity to start. To add more to this answer.

How do I start an activity only once?

It is important to check that the first activity which opens when the app is launched is MainActivity. java (The activity which we want to appear only once). For this, open the AndroidManifest. xml file and ensure that we have the intent-filter tag inside the activity tag that should appear just once.

What is multiple activity in Android Studio?

Even the simplest applications have more than one functionality. Hence, there is often a need to deal with multiple activities. For example, a game can have two activities: a high scores screen and a game screen.


2 Answers

in Manifest Activity property you can give this parameter android:launchMode="singleInstance"

Read in more detail here http://developer.android.com/guide/topics/manifest/activity-element.html

like image 172
ud_an Avatar answered Sep 22 '22 08:09

ud_an


This is your flag! http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_REORDER_TO_FRONT

intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) 

Note the 'addFlags'. Also note that, onCreate will not be called on this Activity when a new Intent is delivered to it. It will be delivered via the onNewIntent().

This does not ensure that there is a single instance of the Activity running. To ensure that, check this out: http://developer.android.com/guide/topics/manifest/activity-element.html#lmode

like image 25
Vikram Bodicherla Avatar answered Sep 18 '22 08:09

Vikram Bodicherla