Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having One Instance Of Activity

Tags:

I have a problem with Activity navigation and the back stack and I was hoping you could clear it up for me.

The Problem

Root Activity >>> SecondActivity >> HomeButton

This then takes me to the home page and from there I select...

Gmail >> Message >> Open attachment in my application >> ImportActivity >> RootActivity

The outcome is a new task being started and another instance of my application being used. This is unacceptable as I don't want two separate tasks running I only want one.

The Desired Outcome

What I want to happen is when the user opens the attachment and ImportActivity finishes what its doing (it has android:noHistory set to true) and calls startActivity(intent), RootActivity is started, but keeping only one instance and the rest of the activities on top of it in the original task (in this case SecondActivity)are removed.

The reason I want this is if the user navigates out of my application after importing the file and then touches the app icon it loads the first task with its back stack and I have two tasks running in which the user can be in two different parts of my application at once.

What I have tried

I have played around with the launch mode but none of them really give me the functionality that I need.

Launch Modes I have tried..

android:launchMode="singleTask" - this just started the root activity again each time it was launched. Even if the user pressed the home button inside my application and touched the App icon the back stack was destroyed.

android:launchMode="singleInstance = doesn't allow any other activites to run in the task.

Also when calling the intent to start the RootActivity I have used the following to no avail.

Intent i = new Intent(ImportActivity.this,TrackingActivity.class); i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(i); 

Is it possible to do what I want?

Thanks in advance

like image 651
StuStirling Avatar asked Dec 12 '12 19:12

StuStirling


People also ask

What is the instance for 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.

Can activity have only one task?

The activity can be instantiated multiple times, each instance can belong to different tasks, and one task can have multiple instances (but only if the activity at the top of the back stack is not an existing instance of the activity).

What is single task activity?

Single TaskIf the activity already exists on another task, no new instance is created, and the Android system transmits the intent information via the onNewIntent() function. At any one time, there will be just one instance of the activity.


1 Answers

You say

Gmail >> Message >> Open attachment in my application >> ImportActivity >> RootActivity 

but that may not be right. In this circumstance, gmail will issue an Intent targeted at the ImportActivity in your app. ImportActivity will execute. However, my reading of https://developer.android.com/guide/components/tasks-and-back-stack.html suggests that ImportActivity will execute as part of the same task as gmail and will be put on top of the back stack for the gmail task, unless you take special steps in the manifest to prevent that or gmail specifically invokes it as a separate task. When ImportActivity finishes, it shouldn't call startActivity(intentForRootActivity) but should just call finish() so that it will be destroyed and the activity from gmail which lies underneath it in the back stack will appear.

If ImportActivity did call startActivity(intentForRootActivity) then RootActivity would just go onto the top of the gmail task and appear on the gmail back stack. Touching home and then the launcher icon for gmail would see RootActivity reappear, hiding gmail underneath.

I think you need android:launchMode="standard" in the manifest declaration of ImportActivity.

The task which represents the older, stand-alone instance of your app will not be modified. If someone touches the launcher icon for your app, the old state of your app will be restored, unaffected by whatever happened in the gmail task.

The document http://developer.android.com/guide/components/processes-and-threads.html is rather vague about how Activities map onto processes here (causing David Wasser to comment on my earlier version of this answer) but it seems that the Activities of gmail will execute in one linux process with one linux user id and the ImportActivity will execute in another linux process with another user id. However, they can all form part of one task with one back stack.

ImportActivity will execute, as part of the gmail task, with the same effective Linux User ID as it would had it executed as part of your standalone app - and different from the Linux user ID that gmail executes with. This sounds unlikely and complicated but seems to be implied by https://developer.android.com/guide/components/fundamentals.html. That makes sense; if ImportActivity needs to get at, say, the user preferences stored for your app, it needs to read the preference file as if it were the user defined for your app, not the user defined for gmail.

I haven't tried executing any of this. If I have hold of entirely the wrong end of the stick, I'm sure someone will soon tell us!

like image 189
emrys57 Avatar answered Oct 05 '22 22:10

emrys57