Send update command to previous activity - android

I have 2 activities A and B
activity A contains a button to call activity B and A also contains a listview.
activity B contains some checkboxes and a close Button
After I click to close activity B I would like to update activity A's listview. What method do you think is good for me to follow now ?

To achieve your functionality, you need to launch activity B using .startActivityForResult() and override the onActivityResult() method to do all the processing you need to do when B has been terminated.

Related

Remove all the previous activites except home activity

How to remove all the activities except HomeActivity.
ex:
A(HomeActivity)->B->C->D
now call B activity again removing C & D activities. I'm sending some data from D activity to B activity through intent. I have next button if i click next button then this process will be done. If I backpress then my process will be D->C->B->A
When you're going from D->B, use intent.setFlag(Intent.FLAG_ACTIVITY_CLEAR_TOP);
It will close all of the other activities on top of it and this Intent will be delivered to the (now on top) old activity as a new Intent.
FLAG_ACTIVITY_CLEAR_TOP
Then you need to create static activity variable in both C & D activity.
Just like this
//create static variable
public static CActivity cactivity;
then initialize this variable in onCreate of Activity.
//initialization
cactivity=this;
Finish that activity where you want
// use this where you want to finish activity
CActivity.cactivity.finish();
Make sure Activity must be running in background other wise you app will crash.
what I would of suggest is a not-so-smooth solution but instead of start activities for all of those replace them with a startActivityForResult, search for the result code and if it is returned from the activity (that is for you to handle of course) then call finish with all activity in the way with the result code until you reach your home activity.

Android, EditText preservation through new activities

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?.

how to update the Activity when press back button in android?

In my android apps i have four activity A->B->C->D and in every activity i call web service then update the list-view and other element from the server so suppose i did delete some data from the database for Activity A and if user access the Activity B and he press the back button then Activity A should be refreshed. I did override onResume method and check the data is updated or not but didn't worked. I also tried another way but not achieve my goal.
my Activity structure is like a following
Class A Is Fragment which is having two tab and each tab contain listview
Class B Is Activity
Class C Is Activity
Class D Is Activity
I don't understand how to call web service again when press the back button and how to update fragment and activity when user press the back button. Please anybody suggest me some answer.
Thanks in advance
you can override the OnBackPressed-method. check the top-answer on that link!!
Android: Proper Way to use onBackPressed() with Toast
Start activity B from activity A with "startActivityForResult" instead of "startActivity".
In activity B you defaultly return RESULT_CANCELED -> so setResult(RESULT_CANCELED). Only when activity B is finished on purpose you return RESULT_OK.
In activity A you then just need to implement onActivityResult(). Depending on the result returned from activity B you can then do specific stuff in activity A, in your case refresh something.
Here is an example: http://www.vogella.com/articles/AndroidIntent/#usingintents_sub

finish() not working properly

In my app, whenever calling the finish() method, wherever it was, I am not taken to the previous activity, rather I am directed to mainActivity.
finish();
My aim is, showing the user the activity just before the current activity he is seeing.
Question 1 : How can I make finish() always take me to the activity before ?
Question 2 : Does this work using another workaround other than finish() ?
Question 3 : How to check the stack of activities and decide accordingly which one to go to ?
If you have written finish in each intermediate activity, that means you are removing the activity from the stack, hence on finishing an activity you are taken to the last non-finished activity, hence write finish() in only that activity which you do not want to see until the same workflow is followed and its onCreate() is called
if you start activity c from b and b from a if you use the back button on your phone at activity c it will go to b and back button press in b it will show a. if you use finish in all the 3 activities what happens is a calls b and a is finished and b calls c and b is finished so when you use finish in c it will not have b to show. so you have to tell where you are placing your finish based on some condition or a button click or just before starting a new activity, post your code and we will help you.

Update the first screen automatically even if it is declared as android:launchMode="singleTask"

I have 2 activities. Activity A and Activity B. Both calling each other though intent. Activity A calls Activity B. Activity B accesses database and sends it back to activity A through putExtra() and getExtra().
Now my activity A is in declared like this android:launchMode="singleTask"
When I go back to activity A I want my this activity A to be updated or refreshed automatically. But to my wonder what I understood on debugging that if I declare an activity as launchMode="singleTask" then it just brings forth the screen to top from the stack. It does not actually go inside the code.
Is the concept what I understood correct ?
The solution I see is have a refresh button and on click of that access the code and update screen. But I do not want to do that. Do you think there is any other alternative ? I do not want to change launchMode="singleTask"
Thanks in Advance.
Try startActivityForResult(intent); and while you have finished in B setResult(RESULT_OK); and finish(); the B activity, and in A onActivityResult(int,int,intent); catch the result code if it is RESULT_OK update your A.
No matter which launchMode, when you switch from Activity B to Activity A, the onResume() method must be invoked. You can put your refresh code there to get your activity A updated.
put your refresh part in the onResume method .Once Activity B completed your onResume method will be invoked in Activity A.

Categories

Resources