My app has a log in MainActivity. By tapping on a link a SecondActivity starts and the user can sign up. After the user completes the form an email is sent with a deep link to activate the account. When the user taps that link the MainActivity starts again indicating that the account was activated and that the user can log in. The problem is that in android 4.3 the previous activities get clear, but not in Android 5:
This is part of the code I'm using when the user taps the deep link:
Intent toLaunchMainActivityAgain = new Intent(this, MainActivity.class);
toLaunchMainActivityAgain.addFlags(toLaunchMainActivityAgain.FLAG_ACTIVITY_CLEAR_TOP);
toLaunchMainActivityAgain.setFlags(toLaunchMainActivityAgain.FLAG_ACTIVITY_MULTIPLE_TASK);
toLaunchMainActivityAgain.setFlags(toLaunchMainActivityAgain.FLAG_ACTIVITY_NEW_TASK);
When you are trying to launch another activity, call below to make sure Android Task Manager doesn't store the activity you are leaving
startActivity(new Intent(MainActivity.this, OtherActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK));
When I want to then go back to another activity from my current one, I simply call
finish();
Related
My app has a service that runs in the background. It wakes up on intent from the user which is typically generated from another application (such as sharing to my app from the web browser). After the service is done performing a task I want it to open an Activity that displays some info about the task that was completed. When the user is done looking at this info, I want them to be able to hit 'Back' or 'Close' and return to what they were doing previously (not within my application).
I've only managed to get the activity to close leaving my App's main activity front and center on the screen. This only happens if the app is still running. If it's been killed then it correctly returns to the web browser where the user intent was generated from.
How can I make the 'Back' button close the activity and return to the previous app from which this entire workflow was started?
EDIT:
This is the code I am executing from within the android service:
Intent intent = new Intent(this, TestActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
When I tap the back button from the TestActivity it returns me to the main activity of my app, not the previous app I was in when the service started TestActivity.
In second line you are using Intent.FLAG_ACTIVITY_CLEAR_TOP which is clearing your previous activity
I got an app which can receive an event (which is basically a message from a service in the background), and when it does, I am invoking this code:
Intent dialogIntent = new Intent(this, MainActivity.class);
dialogIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(dialogIntent);
This code is supposed to return the app to the front so the user can handle the event.
This code works only if the app I'm working on was also the last app the user has been in. The code doesn't work in these scenarios:
1) The user has paused the app, opened another app and then paused it too so the 2nd app is the last in the background - in which case the last app will always open, even though I'm starting my MainActivity. For example, if I've moved my app to the background, opened Android's settings and then an event is received, the Android's settings screen will open instead of my app.
2) The user is inside another app. In which case, the app won't be brought to the front at all.
How can I made the specific app to be resumed from the background no matter what?
Use launcher category as below:
Intent resumeIntent = new Intent(context, MainActivity.class);
resumeIntent.setAction(Intent.ACTION_MAIN);
resumeIntent.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(resumeIntent);
Also add android:launchMode="singleTop" in the manifest of your activity.
You can add android:launchMode="singleTop" in the manifest for the Activity.
I have a problem regarding Android task and intent management.
Scenario
User gets a push with a deep-link into the app.
We show a notification putting the URI into the Intent Data.
User clicks the notification and is taken into the app and redirected to some Feature1Activity described by the deep-link.
User looks around, and backs out of the app.
Later, user opens the app from history (long-press home or multitasking button).
Now the same intent that were used from the notification (with the deep-link in the Intent Data) is used to start the app.
Hence, user is taken into the Feature1Activity again.
Problem:
Starting the app from history (long-press home or multitasking button) does not reset the Task (which it does when launching from app icon).
I understand that starting an app from history is not supposed to reset the task since it is intended to be used as "get-right-back-where-you-were". However, in my case this is an issue since the launch of the app from a notification is a one time thing.
Anyone else encountered this problem? Anyone know any solution?
More in-depth
The intent inside the PendingIntent is built like this:
Intent intent = new Intent (Intent.ActionView);
intent.addFlags (Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags (Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags (Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
intent.setData (Uri.Parse (DEEP_LINK_URL));
I found out about the FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET just this day and really thought that it would git rid of my problem but it made no difference.
There are three activities of interest:
SplashActivity (main launcher & listener of the deep-linking schema -- this activity just redirects either to login or OverviewActivity)
OverviewActivity (authorized user's main activity)
Feature1Activity (any feature that the deep-link is pointing to)
What happens when the user clicks the notification is that the SplashActivity acts as a listener for the schema and converts the deep-link url to two intents to start up OverviewActivity and Feature1Activity using Activity.startActivities (Intent[]).
When I look at the intent from the notification inside SplashActivity it always contain the deep-link in the Data.
One work around
There is a work around, setting some booleanExtra field to the notification intent (for instance "ignoreWhenLaunchedFromHistory" = true) and then check in SplashActivity before redirecting
boolean fromHistory = (getIntent().getFlags() & FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) == FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY;
if (fromHistory && getIntent().getBooleanExtra ("ignoreWhenLaunchedFromHistory", false))
// Don't follow deep-link even if it exists
else
// Follow deep-link
Except that it hackish and ugly, can you see any problems with this work around?
EDIT: The work around only works when I am responsible for sending the Intent with the deep-link. Since no external source can know about the "ignoreWhenLaunchedFromHistory" extra.
From what I get, maybe using android:excludeFromRecents="true"on your manifest (as a property for the Activity declaration) might ameliorate the issue?
I have an application and every new created activity will start an async task to validate the user session. If the session is valid, the application flows continues. If not, the whole activity stack must be cleared and there should be only the login activity. This activity has a "no history" flag so it is never kept in the stack.
I've been trying some solutions provided here: Android: Clear Activity Stack but with no success.
This must works on the lowest android possible, being the least 2.2
Thanks!
I keep my login Activity on the stack. In the onResume() of the login Activity, I check to see if the user has login credentials and, if so, call startActivity for the next screen presented after login. The user does not see the login screen in this case.
When the user presses the logout button, I clear the user's credentials and then this clears the stack all the way back to the login screen:
Intent intentLaunchLogin = new Intent(this, ActivityLogin.class);
intentLaunchLogin.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intentLaunchLogin);
Also, if the user is on the screen presented after the login and they press the 'back' button, I don't want them to go to the login Activity. This code will send the user to the Home screen as would be expected:
moveTaskToBack(true);
Could you do something like is described here:
http://blog.janjonas.net/2010-12-20/android-development-restart-application-programmatically
basically you create an alarm that starts your intent, then you close your app completely.
This is what I always do and works perfectly.
I start the app with the main activity an check if the user is logged in, if he is not logged in launch the login activity like this
void launchLoginActivity(){
/* Move user to LoginActivity, and remove the backstack */
Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
}
It will not allow u to go back
I have many activities in my application.
First is=Main.java
Second is=App.java
third is =Location.java
In main.java i have defined that if there is username and sessionid value stored in preferences then directly move to Location activity else show App.java.I have finished main.java in both cases.Main.java is just a splash screen.
I have defined an exit method in main.java that defines:
`android.os.Process.killProcess(android.os.Process.myPid())`
If someone logout from application then it comes on App.java.To close application on back button i have called Main.exit() which closes application in correct way.
And on location page i have used
`moveTaskToBack(true)`
which closes application.
But if i come back directly to Location page after some time my whole application does not work properly its session id expires.
And if i come from App.java page it works well.
I want to create it like facebook if u r logged in and closes the application it starts from second page.and if u logout then shows the login screen.
Please help me to resolve this.
Thanks.
I had something similar in my app. I wanted a way for the user to "sign out" of the application and not just the activity.
So, I did this:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
System.exit(0);
That code launches the default home screen (exits the app) and then closes all of my activities. When the application is re-launched, it automatically goes through my MAIN launcher activity (which is login).