In my main activity, when you click a button, it launches a new activity (act2), in which another activity (act3) is loaded onCreate (assuming it was launched by that specific button only)
act3 is a listview, selecting an item returns you to act2 and sets a textview accordingly. If you press the back button in act3 it takes you to act2. However, if act3 originated from that specific button and no item is selected I would like for it to take you back to the main activity instead. What is the best way to handle this?
Set the activity result of act3 to RESULT_OK if something was selected before finishing. in onActivityResult in act2, if the result was RESULT_CANCELLED, finish(). If it was RESULT_OK, process the returned value.
Related
How can I preserve data between activities? For instance, I have an edit text and a button in Activity A. I fill in that editText and then click on the button. That button starts a new intent, Activity B. In B there are several buttons, but all of them if clicked on, start activity A again. I want that editText to be still filled in as well as pass specific data to Activity A depending on the button pressed in Activity B.
I know this has to do with onStop() -> onStart(), but couldn't get it to work.
You should start Activity B with startActivityForResult. Then, when u set the result, you should "catch" it in Activity A by overriding onActivityResult().
As for EditText, if you don't call finish() on Activity A - you don't need to override onSaveInstanceState or anything else - system does it for you.
Don't finish the activity in Activity A intent to Activity B. and use startActivityForResult(intent) rather than startActivity(intent) to be able to pass the data back to activity A again. check this answer to how How to manage `startActivityForResult` on Android?.
I have a ListView with some buttons lets say every list has button_1 and button_2 when i click button_1 i will go to next intent but will not finish() the previews activity and then on my present activity there is a button_3 and if i click button_3 the button_2 from previous activity will change too when i go back. now my question is is there a way we can pass a view from another activity or is it possible to call setNotifyOnChange() on another activity?
just call setNotifyOnChange() in onResume() method of the previous activity
I am opening new activity on button click of one activity. New Activity contains image button named go back. Now I want that when anyone click on back button of mobile device, it fires onPause or say close the application but when anyone clicks on go back image button, application does not fire onPause and it goes on last activity.
How to do it ?
If you have called finish() in your first activity while coming to this second activity and if you want to go to first activity again on click event of "go back" button in second activity, then you need to call an Intent to go from second activity to first activity.
But if you do not call finish() in your first activity while coming to this second activity and you want to go to first activity again on click event of "go back" button in second activity then simply call finish() on click event of "go back" button in second activity.
I saw your code from your other question, so you need to remove the finish() from onPause and add finish() to your "go back" button listener.
Do not call finish() if you want to keep it on the stack (in order to go back).
Example:
Activity A starts Activity B, which starts Activity C. (no finish() called yet)
Then when you go back from C, call finish() and you will
see Activity B.
Then when you go back from B, call finish() and you will see Activity A.
This is how you can pass values between different activities.
Intent i = new Intent(A.this, B.class);
i.putExtra("numbers", array);
startActivity(i);
My app starts up at Activity A which contains a ListView. The ListView can have items added to it if the user hits "Add" button and goes to Activity B.
In Activity B they fill out some forms and hit "OK" button which takes them back to Activity A where the new item is added to the ListView.
I have a finish() method after going from B to A -- but NOT the other way around.
So if you hit back three times after adding three items. It will just repeat the ListView (Activity A) over 3 times -- less one item that was added.
What is the best way in doing this? I can't put a finish method on the "Add" Button (going from A to B) because if you are in Activity B, it will close the app instead of taking you back to A -- which I do not want. That is, if the user changes his mind and doesn't want to "Add new item" by hitting "OK" while in B. Is a manual Back button the only answer?
Start Activity B by using startActivityForResult() and finish activity B after filling the form.
EDIT
When you startActivityForResult(), you pass 2 parameters, namely intent and requestcode. After you are finished with the new activity(in your case Activity B) you use a function setResult(RESULT_OK) to signify that the operation in Activity B was successful and then you call finish(). After the call to finish() the Activity B will return to Activity A and will call onActivityResult(int requestCode, int resultCode, Intent data). The parameter requestcode helps in identifying which particular activity/request has returned.
Hope this explanation helps you.
I am new going for the android.I am working on an app in which I have two activities let say A and B.
In activity A I have a list view of some items.I have a button in activity A which takes me to activity B.In activity B I have a seek-bar.
I am using the seek-bar to filter the result of activity A.
I have two buttons in activity B cancel and filter.
After adjusting seek-bar if user clicking filter button than it takes user to activity A and showing filter results.
User can play between activity A and activity B.
Now three different scenario are there for coming back from activity B to A.
By pressing filter button
By pressing cancel button
By pressing phone's back button
After adjusting seek-bar if user pressing filter button then activity A is re ordering and showing filter results. Here I want to save the instance of activity B. so that from activity A if user again going in activity B then I can show the previous state of activity B.(I am able to do this)
In second scenario if user adjusting the seek-bar again and pressing cancel button then Activity A is reordering.Here I do not want to save the instance of activity B and if user again going in activity B from activity A then I want to show the previous state of activity B.(I am not getting how to do this ??)
In Third scenario if user adjusting the seek-bar again and pressing phone's back button then Activity A is reordering but now if user again going in activity B from activity A then activity B is restarting that I do not want, here also I want to show the previous state of activity B.(I am not getting how to do this also ??)
I am stuck with this problem.
Thank you so much in advance.
You must consider each of the cases and manage the activity lifecycle accordingly. You did this just fine, the problem is how to manage it. So, the first step is to study this:
activity and lifecycles
After understanding how the lifecycle of activities is handled by the android os you're on your way: Manage the life of your second activity so that state is mantained by overriding the onPause method (which is called when your activity is no longer in the front of the application) or finish the activity if you don't want to save the state (effectively calling the onStop method.
I would solve this in this way:
in activity A i call the activity B with a startActivityForResult. This way, when the activity B is finished, it's state is not mantained. So, call finish() inside activity B to return to activity A without saving it's state.
When wanting to save the state of activity b, call the activity A so that the onPause method gets called and the state saved.
Hope this clarifies it for you.