Back button issues after forwarding intents via share menu - android

I am working on a login screen for my application. If I open the application, by either running from eclipse or by selecting the application icon installed on the emulator, it will run an AuthUser.class which checks for a valid token on a remote server. If the user is not logged in then the AuthUser.class forwards to Login.class via...
if (authtoken.length() == 0 || authtoken.length() > 0
&& checkAuthToken(authtoken) == false) {
Intent intent = new Intent();
intent.setClass(AuthUser.this, Login.class);
startActivity(intent);
finish();
}
This works great, as when I use the back button on the emulator it will close the app rather than go back to the login screen. Perfect.
Now when I am coming from a share menu, like when I select share icon on an image within the gallery, and then select my application from the popup menu I also forward to the Login.class via...
Intent intent1 = new Intent();
intent1.setClass(SharePictureMenu.this, AuthUser.class);
startActivity(intent1);
finish();
Now when I login from here and it forwards to AuthUser.class as per usual, however, the back button press now takes me back to the login screen, even though I am forwarding and finishing exactly the same in both cases. Obviously I don't want that behavior as I want it to close the app and return to the gallery after login so the user can continue to share images immediately without having to go back through the login screen.
Any suggestions, hacks or otherwise on fixing this would be greatly appreciated.

If you are calling finish() on that activity, before that functions return it will be marked as finished by the activity manager and the user can not return to it. I don't know of a way this could not happen. Make sure you are actually finishing that activity, and that you are not doing something like starting it twice.
Some useful tools for debugging:
The output of "adb logcat" will contain a log for every activity that is started.
The output of "adb logcat -b events" will contain a log for every activity that is finished, with the reason why it was finished (and various other activity operations). You can combine this with the other with "adb logcat -b events -b system".
"adb shell dumpsys activity" will give you the current activity stacks, so you can see the current activities being managed for your app and how they are related to others.

Related

How do I return to previous app after a service generated activity is 'finished'

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

clearing all open previous activities not working in android 5.0

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();

start an activity without show it

I was trying to start activity from a service without showing it to the user, keep it work in background, I was searching a lot about that, and I found two ways to do that
by starting the activity then start the home main screen like this :
// this is for lunch the app
Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.example.some");
// this is for going back
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// lets do it !
startActivity(LaunchIntent);
startActivity(startMain);
by putting a going back function in the activity itself, like this :
context.getActivity().moveTaskToBack(true);
BUT, in these two ways I have problems
in the first one, if the user was on another app, this operation will close his app and get him to home , more than that, some times the my activity not started but just be in the back without working i.e. if there was a song it isn't played
in second one, when the my activity started a black screen will appear for a second before it back to the home or previous user app
So , simply , this is what I want :
I want a behaviour equal to : the user open my app then he press back button, but without show that the app started unless he see the background apps
how to do that ?
For that, you can use an android Service.
See docs - http://developer.android.com/guide/components/services.html

Move back to App after launching another App

I have a situation which I have been working on for close to a week now but can not come up with a working solution.
I have an app, which will launch a terminal window and run a command, the terminal used is Jack Palevich's Android-Terminal-Emulator (source code here) and the code used to launch a window is:
public boolean runCommand(String command) {
Intent intent = new Intent("jackpal.androidterm.RUN_SCRIPT");
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.putExtra("jackpal.androidterm.iInitialCommand", command);
if (mHandle != null) {
// Identify the targeted window by its handle
intent.putExtra("jackpal.androidterm.window_handle", mHandle);
}
startActivityForResult(intent, REQUEST_WINDOW_HANDLE);
return true;
}
Now this works fine, however the issue is that there are a number of times in the apps life that it will need to pass a command to this terminal window, and no user will be interacting with the device (i.e it will need to be automated).
When you launch a terminal window that window becomes the activity in view, therefore while my app still runs in the background any future calls to runCommand() will not happen until my app is brought back to view.
So I need to find a way to have my app become the app in view again once it has called runCommand() and started the terminal. I have tried a number of routes but with no really success, I guess the only really way forward will be to make changes to the terminal app itself, which I am happy to do (I have the source download and tested) but am just a little stuck as to where to look and what to change.
If you need any more info about my app or anything else, let me know!
Thank you

Android Launching or bringing to front another application via Intent

Im having trouble getting this to work, hereĀ“s a quick overview of the idea.
First, I cant change the logic behind this, it was a specific requirement from the customer, I realize that with any tool such as AnyCut it could be bypassed but that doesnt matter really.
My customer offers a suite of apps, the idea is that all applications bellonging to the suite would be launched from a "Dashboard app", so that I only show the Dashboard app in the main launcher and not all app icons.
Lets take two Apps to get the idea solved. The Dashboard App (A) and the Recieving App (B).
I want to establish an intent filter (I think) on app B so that whenever I go into app A, and click the app B icon the app will be either launched or started from where it let of (brought to front).
Is this even possible? If so, how can I do it? I managed to get it to launch by specifically launching one activity in the app using:
Intent i = new Intent();
i.setClassName("PACKAGE_NAME","SPECIFIC_CLASS");
startActivity(i);
But that isnt the behaviour that I want, as it always starts app B in the same spot.
Thanx in advance,
Stefano
Edit: Added some new information. I was taking a look at the DDMS.
If I launch the application from scratch through the main Android launcher the intent is exactly the same as when I leave the home button pressed and then only bring the app to front, what ever activity im in. So I was trying to reproduce, unsucsesfully until now, this intent.
INFO/ActivityManager(1292): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.example.package/.uiPackage.Activity}
This is how AnyCut does it
Intent { act=android.intent.action.VIEW flg=0x10000000 cmp=com.example.package/.uiPackage.Activity bnds=[125,242][235,360]}
Any idea how I could go about creating that exact same intent? I cant even find that flag in the Intent API.
Figured it out, this is how I did it.
Intent i = new Intent();
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.setAction("android.intent.action.VIEW");
i.setComponent(ComponentName.unflattenFromString("com.example.package/com.example.package.activityName"));
startActivity(i);
I'm not quite sure I'm following the expected results you want to see, but the following would launch the app from the dashboard and remove the dashboard from the activity stack leaving the selected app running:
Intent i = new Intent();
i.setClassName("PACKAGE_NAME","SPECIFIC_CLASS");
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
I believe this should start the app as if you were starting any other app.
Please add more information on your logic if this is not what you are looking for.
I think that when you switch activities android's default action is to sort of pause or hold the activity in its state the user left it in last. I know there is a way to make it so that the state is not saved when switching activities but I cant remember it off the top of my head.

Categories

Resources