I have 3 activities A,B and C. Activity A contains a list of items. When we click a row in Activity A, it calls Activity B which describes the item. In activity B i have another list. When I click on a row in it, it calls Activity C.
I have to pass id field from A to B and then to C.
Suppose I have values
Small
Medium
in Activity A.
I am clicking Small from this and going to Activity B and then to C. Everything works fine. The id passed is also correct throughout the activities.
Now from C, I am clicking phone's back button and going to B and then agin clicking phone's back button and going to A.
Now I am clicking Medium from the list. The id is correctly passed to B. But when I reach C, its not going to onCreate() instead to onResume(). There I am getting id value as the old one, of that of Small. But I want the correct id. What may be the reason for this issue? Could anyone please help. Thanks in advance.
I think.. U have to finish the activity when u r handling the back button in Activity C
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
finish();
}
return super.onKeyDown(keyCode, event);
}
The problem occurs because Activity C doesn't get killed, only suspended.
I haven't tested, but I'm fairly certain that if you pass the data via Intent.putExtra, you should get the behaviour you're looking for (I sincerely hope different extra-data causes a new instance to get launched)
Hope this helps,
Phil Lello
As per my knowledge in android activity get destroyed when you press back button(by default).
I suspect you might be dong something wrong while passing data between activities.
But still if you fill it is related to activity is not getting destroyed. You can finish your Activity C in onPause() method. It will resolve your problem.
Related
I have an Application.Suppose there are three Activities A,B,C.
A->B(Sub-Activity)->C(Sub-Activity).
I have set up these kind code to switch to next activity.
setContentView(R.layout.B);
And Problem is When i click back button on emulator or phone while i am on Activity B or C,closes the application window come back to home.
I want that if i will press back button on Activity C it will firstly move to Activity B and then to A,Afterwards if i'll click again to back button then it comes to home-screen or closes application window.
Any methods or something wrong in my code or have to edit ?
You can override the default back press and tell it you reset your content view but you will have to keep track of the stack yourself.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
//code to reset view
return true;
}
return super.onKeyDown(keyCode, event);
}
This said I really recommend you look at allow android to handle the stack in the default manner. Your method will end up causing you headaches in the future.
You need to actually start a new activity and then set the layout in there for the back button to work. Have a look at Tasks and Back Stack
Just setting the view to a new layout is not the same as starting a new activity. From your code, it appears you're just changing the layout and not starting a new activity.
As the method setContentView says, you are just changing the content view for the Activity A. Check The Activity lifecycle and the method startActivity to start new Activity, and read and the Tasks and Back Stack for more information.
As you're using setContentView , you're not switching between activities.
You actually are just changing the content of one and only one activity. Which explains the reason why when you press the back button it closes the application window.
activity A / content A -> activity A / content B -> activity A / content C
if you want to go from an activity to another: you should use Intent and startActivity.
for example you implement a button within the onClick function of the button you implement this:
Intent i = new Intent(this, ActivityTwo.class);
startActivity(i);
and in a different Java file named ActivityTwo.java you implement a class ActivityTwo which extends Activity, within the onCreate method you set a different view, which might be equivalent to content B
you're application scheme will be like this:
activity A / content A -> activity B / content B -> activity C / content C
When you'll be on activity C and you'll press the back button you'll go to activity B.
Suppose assume that there are 3 Activities A ,B and C. On the first launch of my application B is created through A. But on subsequent uses of the application I want the application to launch from B and go to C. How do I do that?
(for example Activity A asks for the number of buttons created and in B so many buttons are created for further activities performed by C. A should be used only on the initialisation and not on further use of app. But the state of the activity created by A in B has to remain the same)
Any references or sample code could be helpful
Thanks in advance.
You can always start your application through activity A. In A, check if it is first time or not. If it is first time then do number of initialization operations and start B. Otherwise directly start B.
You can store isFirstTime flag into preferences.
Use this code in your A, B And C Class
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if(keyCode == KeyEvent.KEYCODE_BACK){
finish();
}
return super.onKeyDown(keyCode, event);
}
This can be accomplished a number of ways, but not too many sure fire ones: the basic approach you will use is a splash screen along with a flag variable stored somewhere. As one of your comments mentions, whenever you enter Activity A, you can always set the flag in a SharedPreferences. When you go back to this activity in the future, you can simply check whether or not the flag is set and then perhaps make a new Intent to start activity C from B. You can also do a similar thing with A, where checking the flag you choose to go to the app. One tricky situation is how to reset the flag. There are a few options, you could conservatively reset the flags in onStop(), which may or may not be a good idea, after you think about the lifecycle. You may also register a boot completed handler, and then reset the flags there, which would essentially let you restart the behavior each time the system boots.
I have an activity A that launches B with an Intent. (B is a MapActivity and has some async code)
in B i have the following code
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
// Log.d(this.getClass().getName(), "back button pressed");
finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
This simply not work. I mean that when I press the back button on the phone, the current activity B disappears and the activity A that launched B is shown.
If I press again on A the button that launch B, I see B is exactly how it was when I closed it. SAME text in textboxes, same Map position... So i think the activity B is not really closed.
How can I close it for real so when I launch again it, activity B is cleas as new born?
EDIT: just to make the question clearer:
I want it to work like this: when the user is on activity B and BACK button is pressed, B has to be closed and destroyed. When the user again launches B from A, B has to be launched as a NEW activity, with blank field, reset map, etc., etc., ... just like the first time the user launches B from A.
Instead of returning true, try returning super.onKeyDown(keyCode, event) after finish().
You can try 1 more thing: specify android:noHistory="true" in the manifest for that activity.
Specifying this attribute doesn't keep the activity on the Activity Stack.
http://developer.android.com/guide/topics/manifest/activity-element.html#nohist
Have you tried lauching B by calling startActivityForResult() in A?
Do all your processing in B and then ensure you call finish on it and this should clear all data in it.
Please let me know if it works.
It's obvious. OnSaveState is called when finishing activity, and this activity is popped out of the stack. Best practice for you to clear the situation is to log all the lifecycle events in both activities, it will help to figure out where to put initializing your B activity.
Yes, and one more thing try researching onNewIntent method in B activity i suppose it will be really helpfull
As i stated in my last comment to the post, #Jianhong given me the correct answer (aslso if as a comment). As he don't copied the comment as answer in time, i add this answer and mark it as the ACCEPTED. Thanks #Jianhong!
Answer:
#Jianhong OMG! you completely right! i checked for this problem in past days but i couldn't find it as... i've not UPDATED from SVN. my coworker inserted lines of code that prefill fields by some static var!
A little info as to why I am attempting to do this: I am using ActivityGroups to open an activity from a tabHost activity and have that new activity stay under the tabs. That part i've got. But when in that new activity, if I use the back button it takes me right out of the tabs activity so I have to click a few times to get back to where I was.
Is there a way to set the back button to go to a specific activity rather than killing the current activity window?
I believe you should be able to do something like this:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
// start new Activity here
}
return super.onKeyDown(keyCode, event);
}
But overriding the expected functionality of the back button is not advisable.
In general, I would advise against that because it breaks the UX. The user expects the back button to kill the entire window, especially since you are using the tabhost. To the user, the entire bunch (tabs and all) is a single activity that he wants to exit when he hits the back button.
If you still want to do it, refer to #onBackPressed(). It is called when the activity has detected the user's press of the back key. The default is to finish the activity, but you can make it do whatever you want. I advise care and caution.
You might find some inspiration from here.
I have 2 Activities A and B. Now these are my objectives.
When I'm in B and if I press the Home button, the state of the Activity should be saved. (No problem with this.)
When I start B from A after step 1 a new instance of B should be created (i.e) Previous state should be discarded.
But in Step 2 the state of B still prevails. How do I accomplish my objective?
I think one possible solution woudl be to pass some extra information inside the starting Intent, when you start Activity B from A (like a boolean value). And in the "onStart()" of B, you check if you can find this extra info in the intent (you get it with getIntent()). If it's not present, that means you do reload the activity's previous state. If it is, then you don't reload it.
refer this url
Android; How can I initialise state in one activity, then have another refresh that?
You don't even need to send a boolean like Scythe suggested. The Bundle savedInstanceState will be null in onCreate for Activity B if Activity A just started it, whereas it will be non-null if you are coming back from a saved state.