I have the following scenario.
I run application A with known packagename that starts Activity A1 as the main activity from the launcher. Now from A1 I launch another activity of this application that possibly is a settings activity or something else, let's call it A2. So our stack is A1-A2.
Now I switch back to the homescreen by pressing home. I start another application, let's call it B. B now wants to be able to switch back to the currently paused A application preserving its activity stack. That would mean we have B-A1-A2 at the end. Is that possible in general? Please note that I only know the package name of A and do not have any more information about application A.
Here is what I already tried without success:
Intent intent = getPackageManager().getLaunchIntentForPackage(PKGNAME);
if(intent!=null)
{
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
finish();
}
But this gives B-A1-A2-A1.
Related
I have following two applications, A & B.
Application A has a service which waits for application B to come in foregrouond. Now, when application B comes in foreground, service of A calls an activity of A which perform certain task.
Now from this activity of A, I want to go back to application B, from where I came to activity of B.
What I am trying right now is:
In service,
String foregroundProcess = getForgroundProcessName();
Now if it matches to a desired process, I am calling an activity:
Intent intent = new Intent(getApplicationContext(),ActivityOfApplicatinA.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("foregroundProcess",foregroundProcess);
startActivity(intent);
Now, from this activity ActivityOfApplicatinA, I want to return back to application B, which is done by:
Intent intent = getIntent();
name = intent.getStringExtra("foregroundProcess");
Log.d(TAG, "The foreground process was:" + name);
Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage(name);
startActivity(LaunchIntent);
finish();
The issue with my approach is that I am not returning to the previous state of application B. It simply relaunch the application B. I don't want this to happen. I want to open application B, with previous state.
Please help me to solve this.
From the docs FLAG_ACTIVITY_REORDER_TO_FRONT:
Intent LaunchIntent = m_context.getPackageManager().getLaunchIntentForPackage(packageNameOfAppB);
LaunchIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(LaunchIntent);
NOTE:
Works only if the application B was launched before and in activity history stack.
I have my launcher activity A which from there starts activity B (based on fb login). I finish activity A before starting B. I also tried using flags such as
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
However sometimes while working with activity B, all of the sudden Activity A pops up. I have no idea where to debug or where to see why A is being brought to the front all of the sudden? Any hints how this can be traced?
I wouldve shared code but sharing thousands of lines wouldn't make sense :)
EDIT:
I tried to simplify the question. Here is more details, in reality There are 3 activities.
A->B->C
A is the launcher activity which takes you to activity B after you choose LOGIN.
B you put the info, then takes you to C. C is where I want to stay on. Somehow, Activity A comes up after some time. If I hit the back button, it takes me back to activity C ( which what I want)
Activity A to B code:
intent = new Intent(LoginRegisterActivity.this, LoginActivity.class);
startActivity(intent);
Activity B to C code:
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Intent.FLAG_ACTIVITY_CLEAR_TASK apparently the flag is ignored for API lower than 11.You can use IntentCompat.FLAG_ACTIVITY_CLEAR_TASK and import import android.support.v4.content.IntentCompat; from support library for work on API 10.
I need your help, I have 2 activities A and B where A is the main activity.
Now B starts the activities from A using startActivityForResult() and from B when I finish it will go back to activity A.
This works fine but the actual purpose was like the Gmail application when you go back from B to A and then start activity B again from A, then A need to start activity with its last screen as i left it.
For example: from inbox->label->draft in gmail how to achieve this to keep data/layout as it is.
Maybe android:launchMode="singleInstance" on activity A and B will get it done?
How to switch Activity from 2 activities without losing its data and current state of data
Ok I have got solution that when every time start your application that time only activity will be created and view and put in stack so the next time you start again this activity just open from background so the activity will available in stack and just brought to front using start activity
From A to start ActivityB
Intent intent = new Intent(context,ActivityB.class);
intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
Now for start ActivityA from ActivityB
Intent intent = new Intent(getInstance(), ActivityA.class);
intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
It just brought front while activity start and put the current activity into background
I have one activity which can be launched from several other activites, along with url filter intents.
On this activity I use the home icon in the actionbar as a back button, bringing the user back to the previous activity (and not as a "home" action). For now I do this by calling the finish() function. This works fine when working from within the application.
However, if launching the activity by an url filter intent, I want the home icon to bring the user to the main activity. Obviously, calling finish() will just close the activity.
So my question is, is there a way to check whether my application stack is empty and then launch the main acivity if true? Or am I attacking this the wrong way?
If your app is launched via url intent filter and it creates its own task, then you can use
if (isTaskRoot()) {
// This activity is at root of task, so launch main activity
} else {
// This activity isn't at root of task, so just finish()
}
EDIT: Added another possible method
If your app is launched into an existing task when it is launched via URL intent filter, then you can do something like the following:
When you launch your activity from other activities in your application, add an EXTRA to the Intent like this:
Intent intent = new Intent(this, MyActivity.class);
intent.putExtra("internal", "true");
startActivity(intent);
When your activity gets launched it can then check for the presence or absence of the EXTRA in the Intent to determine whether it was launched internally or via URL intent-filter, like this:
Intent intent = getIntent();
if (intent.hasExtra("internal")) {
// Launched internally
} else {
// Launched via intent-filter
}
I have two apps, which I will refer to as app A and app B. App B is a single task app and normally, if you start this app from the Home screen and use the Back button, you will not be allowed to return to the Home screen. Now suppose app A needs to call app B using an Intent. If the user then uses the Back button in app B, I really do want them to return to app A. But since I am overriding the Back button, it is not possible to return to app A using the Back button.
How can I return to app A but make sure app B remains running if it was running when app A called it? If app B was not running when app A called it, app B should shutdown (get destroyed) when the Back button is pressed.
I am using Android 2.2
UPDATE:
I tried the following:
#Override
public void onBackPressed()
{
try
{
if (this.returnToClient)
moveTaskToBack (true);
this.returnToClient = false;
}
catch (Exception ex)
{
}
}
I set returnToClient to true if the activity is launched by a calling app by checking some bundle data that gets passed in.
Now if I press the Back button, app B will move to the background and app A comes to the foreground. Looks good. Now if I press the Home button and then launch app B, app B just picks up where it left off. Also good. Now the bad part. If I do a long press on the Home button to bring up the history list and then tap on the icon for app B, the onNewIntent method gets called exactly the same with and with the same data being passed in as though app A had initiated the activity. So what I am guessing is that Android treats launching an app from the Home screen different than it does from the History list. Why? I have no idea. It seems that the history has to do with whoever launched the activity last and persists the bundle data as part of that history. That is weird and it results in unwanted behavior making app B look like it just got called from app A.
Have AppA launch AppB using an Intent with an EXTRA that indicates that it was launched from AppA, something like this:
Intent intent = new Intent(this, AppB.class);
intent.putExtra("returnToAppA", "true");
startActivity(intent);
Then, when AppB traps the "back" key, it can check if it was launched from AppA and if so, it can return to appA like this:
Intent intent = new Intent(this, AppA.class); // Use the starting
// (root) Activity of AppA here
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Setting FLAG_ACTIVITY_NEW_TASK will ensure that a new instance of AppA will not be created, but it will just return to the existing task containing AppA.