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>
Related
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));
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.
I am implementing Tab environment as follow :
There is one class TabScreenABCActivity extends TabActivity,in that i have handled the tabchange functionality.
I have implemented MyTabGroupActivity extends TabGroupActivity. From this class, i have called MyActivity like so:
startChildActivity(getResources().getString(R.string.MyActivity), new Intent(this,MyActivity.class));
Now, MyActivity extends TabGroupActivity, from this class i called two other classes using intent like so:
Intent intent=new Intent();
intent.setClass(MyActivity.this,XYZActivity.class);
TabGroupActivity tab = (TabGroupActivity) MyActivity.this.getParent();
tab.startChildActivity("Tab", intent);
I have also overridden method to go back Activity onBackPressed() method in each activity. But it is not working properly. Can anyone guide me on how to handle it?
You can refer the below link for perfect TabGroupActivity
Handling multiple activies under single tab
This is code for hard back Button of your device. here put your intent.
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
Intent i = new Intent(Activity1.this,Activity2.class);
startActivity(i);
return true;
}
return super.onKeyDown(keyCode, event);
}
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.
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);
}