Android - start Activity and back Issue - android

I have one issue that currently face.
For example as below:
Activity A
to
Activity B
to
Activity B
then in third page, i click back, is go to first page, not go to second page.
i use like below code for go next page in Activity B.
Intent intent = new Intent(act, Activity_B.class);
intent.putExtra("Sample ID", "1");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
i use below code to back in Activity B.
super.onBackPressed();
So does anyone face this problem for back button issue ?
please advice.
thank you.

Remove from your code this line:
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Look here for more information

If you are targetting Android 4.1 (API level 16) or higher you can set the up navigation in the manifest.
And in the activity, when you go back it will go to the parent.
Android developer dox:
Providing Up Navigation

Related

First activity comes to foreground somehow

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.

How to Control Android back stack

Lets say I have
A->B->C->D->E
In android back stack. I want to be able to get back to one of the following:
A->B->C
A->B
A
How can I achieve this? Hopefully without forcing back button clicks.
Using the image and information from the official developers page on Android tasks and back stack you can see that of all other ways to launch an Activity you can ensure such behavior only using the FLAG_ACTIVITY_CLEAR_TOP in your Intent flags.
Your regular back button proceeds as:
But when you specify this flag, you get a behavior like you need, as given by an example at this source:
consider a task consisting of the activities: A, B, C, D. If D calls
startActivity() with an Intent that resolves to the component of
activity B, then C and D will be finished and B receive the given
Intent, resulting in the stack now being: A, B.
Use FLAG_ACTIVITY_CLEAR_TOP flag.
Intent a = new Intent(this, A.class);
a.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(a);
Actually , to go "up" to the activity of your choice, you should use the "up" navigation as used on the action bar:
/** used to handle the "up" button on the action bar, to go to the defined top activity as written on the manifest */
public static void goUpToTopActivity(final Activity currentActivity) {
final Intent intent = NavUtils.getParentActivityIntent(currentActivity);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
NavUtils.navigateUpTo(currentActivity, intent);
}
in order to use it, you must set on the manifest to which activity this function should use (or you could of course set it yourself by changing the code) :
if you use actionBarSherlock, for each activity that you wish to let to go up, use:
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.your_app.activities.MainActivity" />
if you use the android framework (if your minSdk version is API 16 and above), use the "parentActivityName" attribute.
Suppose you are using Intent to move to another activity
Intent i = new Intent(A.this,B.class);
startActivity(i);
this code will take you to the 'B' Activity and when you press Back button it will again take you to the 'A' Activity . If you dont want to go back to activity 'A' you can use....
Intent i = new Intent(A.this,B.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(i);
For more information about Back Stack in Android Follow this link :
http://developer.android.com/guide/components/tasks-and-back-stack.html

Android SingleTask not working

I have a multiple activities in my application. Two of them are a LoginActivity and the second one is a SettingsActivity. The user logs into the Settings activity and logs out of the application from the SettingsActivity. The flow of the application is LoginActivity -> HomeActivity -> SettingsActivity . The user calls the logout from the application in the Settings Activity. I call finish on the Settings activity and create an intent to the LoginActivity. This works fine, but when i press the back on the loginactivity the home activity appears. I want that once the LoginActivity appears the back button should take the application out of the application. How can i do this ?
The answer lies in FLAG_ACTIVITY_CLEAR_TOP.
All you need to do is set the flag to your intent like shown below
Intent i = new Intent(this, LoginActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
finish();
That should pretty much do what you want it to do.
I believe this is what you are looking for:
How to clear the Android Stack of activities?
I wouldnt get too hung up on the fact that it doesnt actually finish() your LoginActivity - this is actually a pretty normal pattern to follow.
I had to use .FLAG_ACTIVITY_CLEAR_TASK for this to work . I read this is supported after API version 11, will these do for all previous versions. Secondly how can i use these from the Manifest file and not the java code ?
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);

Go back to previous screen without creating new instance

As explained in image, flow is something like this. So whenever user click on logo button Activity A should be called. As simple solution we can use this method...
Intent intent = new Intent(activity, activityToStart);
startActivity(intent);
But this activity will create a new activity for my app. but I need to call the same instance of the activity as we move forward in flow diagram. from Activity A to B and then again on B can be called easily by callingfinish() but from Activity C or D, how to come back to A.
I am running out of ideas but not getting any fruitful result. Please help me if you have any suggestion or at any place i am going wrong. Thanks in advance.
To Come Back from D to A, use Intent Flags.
Intent intent = new Intent(activity, activityToStart);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
startActivity(intent);
FLAG_ACTIVITY_CLEAR_TOP, will instead of creating new activity, it will invoke the activity on the stack, and will pop all the activities over the activity being invoked.
Instead of Using
Intent intent = new Intent(activity, activityToStart);
startActivity(intent);
Use
Intent intent = new Intent(activity, activityToStart);
startActivityforResult(intent,1234);
This will make Sure that The Activity A is not Killed and when You finish Your Activity C,Activity A will get Resumed.
Note :- Whenever You create A new Activity,without finishing(Exiting) the Host Activity,The Host Activity is Saved On the Stack in LIFO order
LIFO:- Last In First Out
By making Activity A a "SingleTask" you can achive this. When a Activity is in the SingleTask on clicking the Home button the other activites will be removed from the stack.
Refer these link's for more info on Android Activites...
Link 1 - Android Fundamentals
Link 2 - Another Similar Question
Use ViewFlipper to go and back between different window in the same activity.
ViewFlipper vf = (ViewFlipper) findViewById( R.id.view_flipper);
To go to the next window
vf.showNext();
To go to the previous window
vf.showPrevious();
I am not sure,this is right way or not,but you can give it a try!
You can finish() current activity when you open the new one starting from Activity-B.
i.e.
To open Activity-C => finish Activity-B and start Activity-C
To open Activity-D => finish Activity-C and start Activity-D
now when you will press back,Activity-A will open.

remove activity from stack

I am developing an application in android. I am new in android.
In my application I have a category selection activity from that user have to check a check-box, based on that he will get view on another screen. I have a menu button in 3rd screen in that I have a button for selecting category, when I click on that button its also works fine but when I click back button it will redirect me 2 times at same activity... How to remove this problem? I have used finish() method but its also creates problem its get's me out from the application directly...
I want redirect to selection activity and it should not show me 2 times when I click back button ....
Is there any way please redirect me thank you.
a call to finish() should work so you schould check your code for multiple calling the wrong method or something like this.
Intent intent = new Intent(activity, activityClass.class);
activity.startActivity(intent);
finish();
you should also take a look at the Intent flags:
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
Your question isn't clear enough for me to know if this will solve your problem, but if you include the following attributes on your Activity in AndroidManifest.xml, the so-attributed Activity will never appear in your history list.
android:excludeFromRecents="true"
android:noHistory="true"
As for removing something from the history, I'm not sure how to do that, but I'm interested in the answer!
You should call finish before you call Intent and go to next activity. so the current task is finished and will not be saved in stack and then you intent activity will come on top of stack. If you directly want to go to Selection activity, override onBackPressed() and intent to the activity you want to go to.
There are different flags that you can use to control how your activity interacts with the activity history stack. Two that might be of interest to you are FLAG_ACTIVITY_CLEAR_TOP and FLAG_ACTIVITY_NO_HISTORY.

Categories

Resources