android back to last opened activity when click on app icon - android

I have looking for the some suggestions for back to last opened activity when launch from app icon. But still has some side effects to handle.
LauncherActivity is the entry activity which listen for
<activity
android:name="com.app.ui.LauncherActivity"
android:configChanges="orientation|keyboardHidden"
android:launchMode="singleTask"
android:screenOrientation="nosensor">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Then there is another second activity and third activity.
My scenario is as below:
1. LauncherActivity -> SecondActivity
2. SecondActivity -> ThirdActivity (When launching ThirdAcitivity, SecondActivity will finish itself)
So when at ThirdActivity, user click home key.
If the app is opened from recentTask, then the last opened ThirdActivity will be shown. This is the behavior what i want.
But when the app is opened from the app icon shortcut, then it take to the LauncherActivity. But i want it to be the same behavior as recentTask. If the last opened ThirdActivity is not being destroyed, then it should show this one instead of LauncherActivity.
However if the app force stop and then restart or ThirdActivity is being destroyed, then no need to go back to ThirdActivity. Back to LauncherActivity is ok.
I have been looking for solution. The suggestion of saving last activity to sharedPreference is not good for my case.
I would like to know how to achieve like launch from recent task. Is any one can guide me. Thanks a lot.

In your Launcher activity onCreate() method, write this:
if (!isTaskRoot()
&& getIntent().hasCategory(Intent.CATEGORY_LAUNCHER)
&& getIntent().getAction() != null
&& getIntent().getAction().equals(Intent.ACTION_MAIN)) {
finish();
return;
}
isTaskRoot() is true when there are no activities in activity stack. In your case, if another activity (Third activity in your case) is there in Activity stack, above code will finish the launching activity and will bring Third Activity to screen.

You can handle this by having three fragments in one activity. Let's that activity be your LaunchActivity, then it has LAUNCHER intent-filter and always opens when you click app icon. If you had three fragments in that activity then the last fragment is opening just like when you open your app from recent tasks.

You could also remove launchMode="singleTask". Most apps don't need it.

Related

How to restore previous activity after pressed home button and return to the app

the flow is like below.
MainActivity -> Second Activity
Press home key then device main screen shown.
Enter to app box and click the application icon.
I expect the second activity should be launched but main activity launched.
is there any way that I control it -?!
The default Android behaviour when you press the home button is that the current activity is pushed onto the stack, unless you called finish() in your activity. Whenever you open your application it will launch the activity that's on top of your stack.
I'ts based on your system memory. If you don't have enough memory to keep your activity backgrounded then Android will kill your activity and its state to recoup some memory. I'm thinking that perhaps your phone (or emulated device) is running short of memory.
Check if you are using for that activity, any android:noHistory="true" within your App's manifest. Try removing it.
You filtered your main activity by
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
so it should get launched when you click on the icon in your launcher. I think you should handle your pages with fragments to have the behavior you want.
boolean isSentToBackground = false;
public void onResume(){
if(isSentToBackground) {
finish();
}
}
public void onPause(){
isSentToBackground = true;
}

Android activities launch behavior

My app has two activities. Activity A is the main activity and it has
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
in the manifest. I do not set any android:launchMode for A. Activity A launches Activity B. I press home button in activity B and return to Android menu. If I press my app icon in Android menu, would it launch new instance of activity A or return back to B?
I can see it returns back to activity B and I do not understand why. As I did not set android:launchMode, it should launch new instance of A every time I press icon, shouldn't it?
Activity B launches because when you press your Home button, the application is not "closed", but merely sent to the background. Such is the workflow of Android. If you want to finish Activity B when it is sent to the background, you can call this.finish() in an overridden onPause() function in Activity B.
For more information on managing the Activity lifecycle, check out this very useful tutorial: http://developer.android.com/training/basics/activity-lifecycle/index.html

Android Home button not resuming last Activity

I am trying to solve an issue sort of similar to what was done here (link text)
Except in my case where Activity A, started Activity B, started Activity C, when the user resumes the app after pressing the home button - I want C, the last Activity, to be displayed. Instead what is happening is I am back to A.
Also on a side note I have discovered that even when I exit the application, if I hold down the Home button, my app is still listed as an active app even though in the LogCat I can see that it exited OK.
Also, in case it matters, I did recently add android:launchMode ="singleTask" in order to make some notification logic work correctly. I was seeing the Home button behavior before that though.
I really appreciate any ideas you might have.
[edit - adding some code snippets]
<activity
android:name =".connection.ActivityA"
android:label ="#string/app_name"
android:configChanges="orientation|keyboardHidden"
android:theme ="#android:style/Theme.NoTitleBar"
android:launchMode ="singleTask"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name ="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name =".screens.AvtivityB"
android:label ="#string/app_name"
android:configChanges="orientation|keyboardHidden"
android:theme ="#android:style/Theme.NoTitleBar"
android:launchMode ="singleTask"
android:screenOrientation="portrait" >
</activity>
<activity android:name =".screens.ActivityC"
android:label ="#string/app_name"
android:configChanges="orientation|keyboardHidden"
android:theme ="#android:style/Theme.NoTitleBar"
android:screenOrientation="portrait" >
</activity>
Then here is how I kick off each Activity:
In Activity A:
private void startActivityB()
{
Intent activityBIntent = new Intent(this, ActivityB.class);
this.startActivityForResult(activityBIntent , ACTIVITYB_TAG);
}
Activity B actually has several Activities that it chooses from (it has a list of Intents) to display based on input it receives but launches them all the same way as Activty A launched B. Once the first Activity is displayed the user swipe the screen to navigate to the left or right screen. When the user swipes left or right, the current activity puts info in its return Intent that Activity B uses to display the next Activity. Also since logic outside of the user's control could prompt a change in the screen displayed, each Activty calls finish on itself in onStop to avoid back-button issues.
When you long press home it is showing a list of recently active applications, not applications that are currently running. So you should still see your app in the list, even if it is closed.
As the docs note:
The other modes — singleTask and
singleInstance — are not appropriate
for most applications, since they
result in an interaction model that is
likely to be unfamiliar to users and
is very different from most other
applications.
I'd imagine that your using singleTask is affecting the history stack, and thus preventing Activity C from being remembered. If you remove that from Activity C, does it resolve the problem?
Otherwise, I'd look at what you are doing in onPause/onDestroy etc for Activity C. Are you doing something that would cause it to finish or close?
Do you get this behavior only when running from Eclipse?
Check your "Run Configurations". There is a "Launch Action" that can be set to "Do Nothing".
Matt
I'm not sure this is what it happening to you, but the answer below solved my problem and I wanted the same behavior as you - that is, getting to the same activity when re-opening after home has been pushed (though in my case, I did actually want the first activity, just not a new one):
Save activity state in android when home button pressed
Now I realize you want to keep the activity stack intact and not go to the first activity like me. But if it is the same problem, then it is not from the fact that the activity stack is torn down, but because a new one (with the opening activity) is added when re-opening after home has been pressed on first install. I hope this is fairly legible, even though I (clearly) found it difficult to explain.
Edit: This is better described here and here.

Strange activity stack behavior when using MapActivity

I have the following activity structure in my application
A simple "splash screen" activity is started when the application is fired up (let's call it "Splash"). This activity starts the main activity when the user presses a button (I will call it "Main").
Main can in turn start two activities from the menu. The first activity presents a simple form (let's call this one "Form"), the second is a MapActivity that presents a map (it is called "Map").
Main, Form, and Map are declared exactly the same in the manifest:
<activity android:name="fully qualified activity class"
android:screenOrientation="landscape"
android:configChanges="keyboard|keyboardHidden|orientation"
>
<intent-filter>
<action android:name="android.intent.action.DEFAULT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
When Main is active and I start Form and press "back", Main comes up again. Pressing "back" again brings up "Splash". Nothing strange here.
Now comes the strange part: when I am in Main, start Map, and press "back", Main comes up as expected. But pressing "back" again just restarts Main. A second press on "back" is needed to bring me back to Splash!
So it seems that starting the Map activity somehow results in Main ending up on the activity stack twice while starting the Form activity does not!
Both Form and Map are started like this:
startActivity(new Intent(this, MyActivity.class));
I don not catch the back key in any activity.
Any clues on what is going on or how to debug this?
I think I have seen this behavior before in one of my projects. Try setting the launchMode property of your launcher activity ( in your case Splash ) to - android:launchMode="singleTask" in the manifest file.
This should help clarify: http://developer.android.com/guide/topics/fundamentals.html#acttask

How to clear stack back to root activity when user leaves application?

I have an application with 2 activities, LogonAct and MainAct. LogonAct is a logon activity which I want to force the user to go through each time they return to the application. I've set android:clearTaskOnLaunch="true" on LogonAct.
When I first start the app I go through this sequence of screens,
Home -> LogonAct -> MainAct -> Home
I then follow this sequence,
LogonAct -> Back -> MainAct
Why is it bringing me back to MainAct? Shouldn't that activity haven been closed since LogonAct has android:clearTaskOnLaunch="true". I expected to be brought back to Home when I hit the Back button from LogonAct.
Relevant snippets from AndroidManifest.xml,
<activity android:name=".LogonAct"
android:clearTaskOnLaunch="true">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".MainAct">
<meta-data android:name="android.app.default_searchable"
android:value=".SearchResults" />
</activity>
I'm using 1.5.
Any help appreciated.
You can do following:
1. set clearTaskOnLaunch = "true" in AndroidManifest, in declaration of main activity
2. in activity that must close:
#Override
public void onBackPressed(){
moveTaskToBack(true);
}
so if user presses back - it comes back to homescreen
if user launches aplication again - task stack clears and he comes to root main activity
The docs for android:clearTaskOnLaunch mention that this attribute applies "whenever [the Activity] is re-launched from the home screen".
However, in your case you're pressing the Home button to return to the Home screen, rather than pressing the Back button. This means your application isn't actually relaunched because the MainAct was not "finished". That only happens when you press Back (or if Android kills the task to save resources etc.).
As you only have two activities in your application, you could set the android:noHistory attribute on MainAct, thus ensuring that users can never return to it and must pass through the LogonAct.
As an aside, it seems a bit annoying to force users to re-login every time they navigate away from the app (for example when they receive a phone call).
You could retain a session token with timeout in your app's persistent storage, or hold a network connection open using a service if that's how your app works — but of course that's up to you and your requirements. :)

Categories

Resources