I've an app with 3 activities
LoginActivity
ActivityA
ActivityB
User starts with LoginActivity. After successful login, he goes to ActivityA. ActivityA invokes ActivityB using startActivityForResult and processes the response using onActivityResult.
If user presses 'Home' button from ActivityA or ActivityB and relaunches the application, I want to take user back to LoginActivity
I tried playing around with onRestart and onResume. Both of these are called when ActivityA is reinitialized via home screen or onActivityResult (when user comes back from ActivityB).
How can I implement this requirement?
PS: I checked similar questions on SO and did not find something that matches my requirement.
Thanks.
I will redirect you to this question here. I think it should answer your question.
When you declare your intents to start activity A and activity B, try using the nohistory flag e.g.
Intent intent = new Intent(this, ActivityA.class);
// do not keep this intent in history
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
Then when you relaunch, it will go back to the login activity.
Try writing this in your AndroidManifest file for each activity
<activity android:launchMode="singleTask"
android:name=".LoginActivity" />
<activity android:launchMode="singleTask"
android:name=".ActivityA" />
<activity android:launchMode="singleTask"
android:name=".ActivityB" />
Related
In my app, I want to prevent the user from going back to login activity after logging in, but I don't want to close the app. For example if the previous activity is login activity, I don't want to do anything when the user presses back, just stay in the current activity/fragment.
add finish(); in login activity and add onBackPressed in your next activity
#Override
public void onBackPressed() {
}
Add android:noHistory="true" in the manifest of your LoginActivity
<activity android:name=".LoginActivity" android:noHistory="true">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
The correct way to do this is to call finish() in the Login activity when it completes and launches the next activity. For example:
Intent intent = new Intent(LoginActivity.this, NextActivity.class);
startActivity(intent);
finish();
This would prevent the user from going back from NextActivity to LoginActivity since calling finish() destroys LoginActivity and removes it from the back stack.
There is no need to remove onBackPressed from NextActivity, and that may have unintended consequences (for example, if they navigate from Login -> Next -> Other -> Next then click back, they would expect to go back to Other). Disabling onBackPressed in Next would prevent that.
I have a problem with an activity which is started with FLAG_ACTIVITY_REORDER_TO_FRONT is not recreated and present on the screen after FLAG_ACTIVITY_CLEAR_TASK.
So the step is:
Open app, enter Welcome Activity.
Finish Welcome activity and start activity A without specific intent flag.
Start activity B with FLAG_ACTIVITY_REORDER_TO_FRONT from activity A (new instance of B is created and now stack became A->B).
Start activity A from B with FLAG_ACTIVITY_REORDER_TO_FRONT (existing A instance brought to top of stack so stack became B->A).
And under some condition, I need to start over from the beginning (just like another normal app launch), so started Welcome activity using FLAG_ACTIVITY_CLEAR_TASK.
So the app will enter phase 2 again after phase 1, which is what I expected, but then, if I try to start activity B again with FLAG_ACTIVITY_REORDER_TO_FRONT from activity A, there is activity B's callback 'onNewIntent, onStart, onResume' in a row, but it doesn't present on the screen.
It looks like to me that there is still the previous instance of activity B somewhere but not showing to me.
I don't specify launch mode for either activity A or B.
I know that document says about FLAG_ACTIVITY_CLEAR_TASK that "This can only be used in conjunction with FLAG_ACTIVITY_NEW_TASK.". And use them together does solve my problem, but then if I click home button to put app background and then launch again, it will become another app launch (enter phase 1) but not back to the previous top activity.
So my questions are:
When I use FLAG_ACTIVITY_CLEAR_TASK to start welcome activity, I don't see onDestroy of either activity A or B, is it by design?
If they are not destroyed, where do they stay and can I have a reference to them?
How can I make activity B presented on the screen after all of these steps?
Code for phase 5:
Intent i = new Intent(A.this, WelcomeActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
Thanks in advance.
AndroidManifest.xml
<activity
android:name=".activity.WelcomeActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
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=".activity.A_Activity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:screenOrientation="portrait"
android:theme="#style/BaiduMapTheme.MainMap"
android:windowSoftInputMode="adjustPan" />
<activity
android:name=".activity.B_Activity"
android:configChanges="locale|fontScale|keyboard|keyboardHidden|layoutDirection|mcc|mnc|navigation|orientation|screenLayout|screenSize|touchscreen|uiMode"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateVisible" />
OK, now I think I have those questions because of my incorrect using of the intent flags.
First of all, I should use FLAG_ACTIVITY_CLEAR_TASK and FLAG_ACTIVITY_NEW_TASK at the same time as the document says.
Then everything looks normal and as expected, except one that: if I put the app into background and then click the launch icon again, the WelcomeActivity will be created again.
Previously I thought my existing activity stack is cleared and a new WelcomeActivity is created, but it turns out the existing activity stack is still there, but an extra WelcomeActivity is created, so what I need to do is add some extra flag in WelcomeActivity to determine if it's created in this condition, if yes, then just call finish() in onCreate(), then I can back to the previous activity I was in before enter background.
I have LoginActivity where after successful logging I start MainActivity via intent and finish LoginActivity.
I press back button and then open app via icon and It shows me MainActivity but if I open app from recent apps list after pressing back button so I see LoginActivity
I've checked if LoginActivity was destroyed
How can It be?
manifest
<activity android:name=".LoginActivity" />
<activity android:name=".MainActivity"
android:launchMode="singleTask" />
start MainActivity
Intent intent = new Intent(getActivity(), MainActivity.class);
mProgressDialog.dismiss();
startActivity(intent);
getActivity().finish();
Remove android:launchMode="singleTask"
Why you are adding launchMode, adding this you will be able to get its instance only once. Let default be "Standard", for more information, please have a look at the documentation.
Docs say:
The "standard" and "singleTop" modes differ from each other in just one respect: Every time there's a new intent for a "standard" activity, a new instance of the class is created to respond to that intent. Each instance handles a single intent. Similarly, a new instance of a "singleTop" activity may also be created to handle a new intent. However, if the target task already has an existing instance of the activity at the top of its stack, that instance will receive the new intent (in an onNewIntent() call); a new instance is not created.
So, you wouldn't need singleTask launch mode. Apart from this, I can't see <intent-filter> for your LoginActivity as being MAIN action and LAUNCHER category.
When I press the arrow back I want to navigate to my previous screen(where I came from). So if I'm in MainActivity and go to setting I want the arrow to point back to MainActivity, but if I'm in another Activity I want it to go back to that activity.
<activity
android:name=".SettingsActivity"
android:label="#string/action_settings"
android:parentActivityName=".MainActivity" >
</activity>
How do you start settings activity?
Just use startActivity(intent) in main activity and don't call finish() method. It should be handled automatically.
sample code:
Intent intent = new Intent(MainActivity.this, SettingsActivity.class);
startActivity(intent);
just comment what's your problem exactly and I'll edit my answer.
I have an android application with several activities .Each and every activity has Application Icon in action bar which helps user to return back to main activity directly instead of pressing back button.My problem is that when I use the icon to start my home activity it does not uses the previous instance from the stack and start creating it again.
My Action bar app icon code is :
startActivity(new Intent(this, DashBoard.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
this above code starts Dashboard activity and calls its both onCreate() and onResume().But If I uses back button to return to this activity from any activity it just calls onResume().
Activity definition from manifest file:
<activity
android:name=".DashBoard"
android:configChanges="keyboardHidden"
android:label="#string/app_name"
android:screenOrientation="portrait" >
</activity>
Why is this happening?Am I missing something to prevent it from not creating it again?Please help
Thanks
Use setFlags(), instead of addFlags(). You are on right track. Use the following code.
Intent intent = new Intent(this, DashBoard.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
Remove the FLAG_ACTIVITY_CLEAR_TOP.