Using noHistory=true but opening previous activity when Back is pressed - android

I've been use
android:noHistory="true"
in my code so that I don't have many activities open at the same time.
The problem is the following:
Consider that I navigate navigate from Activity A (HomePage) to Activity B (noHistory=true) to Activity C. From Activity C, if I press the (hardware) back button, it takes me back to Activity A, not B. This is android:noHistory="true" , right? Is there a way to go back to Activity B and load it from scratch instead of going all the way back to Activity A?
Thanks!

You could just use something like this to go 'back' to any activity you'd like.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if(keyCode == KeyEvent.KEYCODE_BACK)
{
Intent intent = new Intent(startActivity.this, returnActivity.class);
startActivity(intent);
return true;
}
return false;
}

I ended up doing #Chandrakanth's method (thanks!); I passed extras to Activity C that will tell it what activity B to open when I press the back button.

Related

Strange activity start order - Android

In my android application I have 3 activities A,B and C
Activity A is the launcher activity of my application, inside it there is a button with the following code when clicked:
startActivity(new Intent(this , B.class));
finish();
in activity B I have a button that starts activity C:
startActivity(new Intent(this , C.class));
In activity C, I need to finish the activity when the home button pressed:
public boolean onKeyDown(int keyCode,KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_HOME)
{
finish();
return true;
}
return super.onKeyDown(keyCode,event);
}
Now I expect that the top activity in my task is activity B, but when I tap the app icon from launcher activity A is started, so it seems the whole task is ended somehow.
Can someone explain what is going on and why am I getting this behavior?
Insert this code into your first activity and call it inside onCreate(...)
private void killIfIsnotTaskRoot() {
if (!isTaskRoot()
&& getIntent().hasCategory(Intent.CATEGORY_LAUNCHER)
&& getIntent().getAction() != null
&& getIntent().getAction().equals(Intent.ACTION_MAIN)) {
finish();
return;
}
}
... 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. In other circumstances — for example, if an existing instance of the "singleTop" activity is in the target task, but not at the top of the stack, or if it's at the top of a stack, but not in the target task — a new instance would be created and pushed on the stack.
take a look at this link How to prevent multiple instances of an activity when it is launched with different intents
In the code you posted, activity B is trying to start activity B, NOT activity C.
in activity B I have a button that starts activity C:
startActivity(new Intent(this,B.class));
should be:
in activity B I have a button that starts activity C:
startActivity(new Intent(this,C.class));

How to finish activities up to one specific activity?

For example, I opened activities A, B, C, D.
I want to finish D and C and return back to B.
I don't want to open activity B with clear tasks and new task flags. I want to keep activity A too so user can return from B to A with back button.
How can I achieve this?
Intent intent = new Intent(getApplicationContext(), B.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
And in B activity:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK))
{
finish();
}
return super.onKeyDown(keyCode, event);
}
I think you can add this.finish() in your activity method onDestroy() in C and D classes, then if you go from C to D, Android will finish this Activity and you will go directly from D to B, instead of D to C.
I hope it will help you.
Make a global static boolean say closeActivity. Set it to false by default. When D is ended set the variable to true. In onResume of activity C check if variable is set to true and if true, call finish(). Do this until activity B is reached and in onResume of B set the boolean to false

Bring a certain activity to front

Let's say: I have 2 classes MainActivity and SecondActivity
At first, my app starts with an instance of MainActivity. In MainActivity, there're several buttons which will start a SecondActivity if they're clicked. And everytime start a SecondActivty, I put a diffirent parameter by putExtra function to that SecondActivity, so every SecondActivity will display every different content.
And I used this code in SecondActivity:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
moveTaskToBack(true);
return true;
}
return super.onKeyDown(keyCode, event);
}
So everytime back button is pressed in a SecondActivity, it won't be destroyed.
Now, for example, I have this scenario:
MainActivity ==click_a_button==> SencondActivity(instance1)
==press_back_button==> MainActivity ==click_another_button==> SecondActivity(instance2) ==press_back_button==> MainActivity
(*)
Stop at step (*): from here (in MainActivity), user presses the button which started instance1, my question is how to bring exactly that SecondActivity(instance1) to front?
P/S: I read about Intent.FLAG_ACTIVITY_REORDER_TO_FRONT but don't know how to use it in my case.
Set the lanchMode of Your SecondActivity to SingleTop.
<activity
android:name=".SecondActivity"
android:launchMode="singleTop" >
</activity>

How to set onBackButton listener to an activity

I have a Class A which runs activity via startActivityForResult by passing Intent to it. In other class, lets say B I get this Intent and recreate activity by it. How can I listen events for that activity, e.g. activity which was started for result is running and user pressed "back" button so I want to do some action.
How can I do this?
Thank you on advance.
Activity in which i recreate instance of object is not derived from Activity class. It is just ACTIVITY. So I have only object. is there any way to do such stuff with instance of class but not a class?
You should override the method : onBackPressed() of the Activity class.
In the activity, where you want to act on "back" button, simply override onKeyDown (or onKeyUp) method:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
//do whatever you need for the hardware 'back' button
return true;
}
return super.onKeyDown(keyCode, event);
}
Keep in mind that if you want the "back" key to still end your activity, then you'll need to include
setResult(result); //if you want to pass a result to activity A
finish();
somewhere in that conditional before return true;
You can override onDestroy and put the code there.
Another possibility (and may fit your needs better) is to override onBackPressed.

How to clear stack history?

Consider am having an application containing activity A,B,C. A is launched from the launcher and B is launched from A. B has a button. My requirement is on clicking button on B the present history of the activity Stack A->B should clear and the history stack must contain only C. Is it possible to do ? If so plz advise me...
Thanks in advance !
Although tedious, this can be done by launching using the Activity methods startActivityForResult(), setResult(), finish(), and onActivityResult().
In pseudo-code:
A: startActivityForResult(B)
B: startActivityForResult(C)
C: startActivity(D); setResult(CLEAR); finish()
D: ...
B: (onActivityResult) setResult(CLEAR); finish()
A: (onActivityResult) finish()
If you're willing to change your architecture a bit a more "natural" way to do this is to use FLAG_ACTIVITY_CLEAR_TOP for an easy way to go from A, B, C to just A.
A third way is to set A, B, and C to use noHistory, but then you would lose the ability to back out of C into B or A.
My "solution" is to override behaviour of Back button in activity C, so that it goes to phone launcher, not back the activity stack. This way, activities A and B remain on the back stack, but user has no way to navigate back to them. So the net result is same as if activity C was root activity.
/**
* Override "Back" key on Android 1.6
* Don't want user going back to Login or Register forms.
*/
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
goHome();
return true;
}
return super.onKeyDown(keyCode, event);
}
/**
* Override "Back" key on Android 2.0 and later
*/
#Override
public void onBackPressed() {
goHome();
}
private void goHome() {
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
}

Categories

Resources