I have Activity A which has editTexts and from activity A, a user can go to activity B and fill out more information. Once the user is done on activity B, they press a button and come back to A. I would like to come back to A with the information that the user had filled in to be already there and not have to fill in the information again.
Currently, this is my code for coming back from B to A.
Intent intent = new Intent(NewAddressActivity.this, CreditCardActivity.class);
intent.putExtra("newAddressEntered", true);
intent.putExtra("newAddress", (Serializable) newAddress);
startActivity(intent);
With this code, when the user comes back, the fields on activity A are empty.
*Note - I need to pass data from B to A when going back.
Any tips on how to do this properly would be greatly appreciated.
Thanks
You are on the right track. You can do that using .putExtra
You can pass extra data via the intent using intent.putExtra("key",
text_field.getText().toString()) on the intent before you send it (in
the first activity) and getIntent().getExtras().getString("key") in
the second activity.
This is assuming text_field is your EditText you want to pass the
value from. You can change "key" to whatever you want, too.
You're actually not "coming back", but starting a new empty intent, thats why the fields are blank again. Instead, you should just call:
finish()
Regarding filling related fields from different Activities, I wouldn't recommend that, but if you need to keep it that way, you may have a singleton class to store all field values for instance.
Also, you could send B fields to A as activity results.-
http://developer.android.com/training/basics/intents/result.html
But still, I would recommend keeping all related fields in one Activity.
Change the launchmode of your Activity A (CreditCardActivity) to singleTop in manifest file and try. The documentation says
If an instance of the activity already exists at the top of the target task, the system routes the intent to that instance through a call to its onNewIntent() method, rather than creating a new instance of the activity.
So your activity should retain old values.
Related
I have few questions about passing data to another activity. Let's say I have three activities I called them
first activity
second activity
third activity
When app is enabled by user, the user is in the first activity. To go from the first activity to the third activity user must also go through the second activity. The workflow
first activity (user push button) ->
second activity (user push button) ->
third activity
Let's assume I need data from the first activity in the third activity, so I need to pass my HashMap with my data from first activity to the second activity and then to the third activity?, is there any way to pass data directly from first activity to third activity but like I said, user has to go also through second activity.
I'm doing this in that way...
Intent secondActivity = new Intent(this, secondActivity.class);
secondActivity.putExtra("myHashMap", dataHashMap);
this.startActivity(secondActivity);
then in the second activity I have this part of code
secondActivity = getIntent();
dataHashMap = (HashMap<String,
String>)secondActivity.getSerializableExtra("myHashMap");
and then I'm repeating this upper two steps for the third activity. Is this correct way? because looks a little bit confusing and time consuming. Also there is another problem, now if I am in the third activity and I want to go back to the second or first activity I have always pass some HashMap because activities expects this type of data. Is there a way to sometimes return to a previous activity but no longer pass any data (HashMap in my case) if it is not needed?.
secondActivity.putExtra("myHashMap", dataHashMap); //- I would like to sometimes skip this step
I tried to this code below to handle with this but it does not work, my app keep stopping/crashing because second activity expects hashMap data from the third activity.
secondActivity = getIntent();
if(secondActivity != null){
dataHashMap = (HashMap<String,
String>)secondActivity.getSerializableExtra("myHashMap");
}
Thanks for answers, regards.
You can use a variable that is placed inside Application class instance. This variable is shared for all activities and other elements of the application.
You have several options. Firstly, you said in a comment that you only need one element. In that case you can just pass the one element as an "extra" in the Intent, instead of passing the entire HashMap.
Secondly, I don't understand why you return from the third Activity to the second Activity by calling startActivity(). You should just call finish() and that will return you to the second Activity.
If you need to return some data from the third Activity to the second Activity, you should start third Activity using startActivityForResult() and set the data to return in third Activity using setResult() and the data will be returned to second Activity in onActivityResult().
Another option is to do it exactly as you have done it, but also copy the hashMap into the Intent as an "extra" each time you call startActivity(). If you only have a handfull of entries in the hashMap it won't make any noticeable difference in performance.
So I have a home button on my App Bar that I implemented which can be clicked from pretty much anywhere in the App, and the exact code in the listener for menu items is
if (id == R.id.button_home) {
startActivity(new Intent(this, MainActivity.class));
return true;
}
As the code would suggest, this just creates a new activity taking the user to the home page however I have two concerns.
1. The potential loss of information, since I currently do not pass any data and it is a new activity.
2. I don't believe that I'm actually destroying any of the previous activities. Could this lead to decreased performance and slow down the application, as you'd end up with redundant activities being retained?
To answer your questions
If you don't pass any information, the data will not persist in whatever state you have it if you were to call a new Intent over to that activity. We can't know exactly what will be lost though, without knowing what you have in there. The safest bet would be to store that data locally in a sqlite3 db, and call it up every time the activity loads, or if it's not that much information, load it up in a shared preference.
Like Time says, you don't create a new activity. You have already done that in the manifest. What you're doing with an Intent is calling the activity that has already been created.
Depends on what do you want to do. If you changed some configuration it's ok to do that with adding flag Intent.FLAG_ACTIVITY_CLEAR_TOP to your Intent instance.
Or, if you just want to remove Activity from the stack, just call onBackPressed().
How about launching the activity with
Intent.FLAG_ACTIVITY_REORDER_TO_FRONT
more about it here
It brings your activity to the top of the activity stack if it already exists or creates a new one if it doesn't.
You shouldn't retain Activities in the first place. Androide lifecycle methods will take care oft handling Apps ging to background. As long as you don't cause Context leaks, you should be find.
The next thing to keep in mind is the fact that you don't create a new instance with your Intent call. The system decides what to do ( hence " intent").This may include jumping back to an existing instance.
Have a look at the Intent flags ( http://developer.android.com/reference/android/content/Intent.html) and choose your desired behavior ( think about back button as well)
So yes: this is perfectly legal if it is the behavior you want
You can use sharedpreference to store the data or you can pass the data with intent.
You can clear the activity stack:
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
I have an app which have HomeActivity and 4 activities A,B,C,D.
I want when clicking on button start_activity_A_btn in HomeActivity to star activity A, and A starts B, B starts C, C starts D, then done button which takes me to HomeActivity.
NOTICE : in every activity (A,B,C,D) I have some data to save and get back to the HomeActivity after pressing done button.
What you need to do use the following flag in your intent (please check the link, it explains a similar situation to the one you are facing): FLAG_ACTIVITY_REORDER_TO_FRONT. So, in your activity D, in the onClickListener for the done button, here's the code you'd have to use:
Intent intent = new Intent(this, ActivityAname.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
intent.putExtra("data", dataYouReceiveFromABCD);
startActivity(intent);
This will get your A Activity to resume.
Now, regarding the "data" you will just have to keep accumulating this data in a String using some separator if that's possible (since you haven't told us what this data is exactly), so if it was a username and a password you could separate the two using a random combination of characters that will possibly never occur ("246#$^") and then just keep creating a string that you keep building in A, B, C and D and then finally in D you put that String as an extra in the intent (check the code I've posted above). If it's some other sort of data then you could perhaps serialize it if that helps. However, if you do use a String with a predetermined separator then all you will have to do is in Activity A you will have to use the following code in the onResume() method of Activity A.
if(this.getIntent().getExtras().getString("data") != null)
{
String data = this.getIntent().getExtras().getString("data");
//do some stuff here with that data
}
If you need data returning an Activity, you should use startActivityForResult to start ABCD. This works very much like your HomeActivity is opening up some dialog, and once the Activity finished (by pressing done or cancel, depends), you get onActivityResult in your HomeActivity.
There are plenty of questions about activity lifecycle, but I couldn't find the suitable one.
I have an Activity (named A) which shows the cars from one user. Internally, the activity stores userId into a variable to load user's cars in the list. userId comes from previous activity.
When user presses the 'new car' button on A, Activity B is launched. User fills the car's form and presses save. User's new car is persisted to database.
At this point, which method must be called to return to activity A from B?
Should I call finish()? Then, user returns to A and B is destroyed. But, what happens if A was destroyed before? How can A be restored if userId is not present?
Maybe calling startActivity(A) with userId as intent extra? But then, I'm creating a new activity!
Other options?
I'm sure somebody would help to clarify these concepts. :)
Edit:
See comments of accepted answer for more information.
finish should be called to return.
1) if A was destroyed it will be recreated by android, unless you called finish() on it. If you saved its state properly there shouldn't be a problem at all.
2) This would just move Activity A back to the top of the Stack or creting a new Activity, depending on what flags you add to the intent.
3) I'd use startActivityForResult in Activity A to start B and then let the user enter the values, put set the result in B and call finish().
I am new in android and I have total 6-7 activities in my application. I want to know how can I manage my activities properly means when I move to the A->B->C->D like that. Then how can I move that the stack of these activities not created.
On moving from one activity to the other I am using the below code:
Intent intent=new Intent(current.this,next.class);
startActivityForResult(intent, 0);
And now if I want to move back on the earlier activity I used the code as:
Intent start = new Intent(current.this,next.class);
startActivity(start);
finishActivity(0);
Is there a special reason that you don't want to use the activity stack and let the activities handle themselves?
The Android system has done a very good job with the activity lifecycle. It allows you to start an Activity from different places without confusing the user because the back button will bring the user back to a different activity.
If you don't have a very good reason to not use the Android guideline try to stick to the way the system is doing it. Every other thing will only give you problems.
You are starting activities for a result but how I understand you you will never return to them.
You can start an Activity and after that just finish the current Activity. That way the activity will not be put on the back stack. Now you need to listen for back button pushes and create the activities that you want to bring the user to.
If you want to move from Activity A to D like going to the start/home screen of you app you do the following:
Intent goBackToA = new Intent(context, StdActivity.class);
goBackToA.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(goBackToA);
The flag FLAG_ACTIVITY_CLEAR_TOP will tell the system that if the backstack contains an instance of the Activity this activity will be shown and all activity that are between the current activity and the target activity are removed from the backstack. This allows you to go back to a home activity without creating huge loops that the user can move through with the back button.
To move back to the previous activity you don't have to create a new intent, you can simply call this.finish() on the one that should dissapear.
To move back to the previous activity you don't have to create a new intent, you can simply call this.finish() on the one that should dissapeear or you can press Back button to see the previous Activity .
whenever you want to navigate from one class to another use this code, may be this help you to navigate the Activity,
Intent nextpage = new Intent(CurrentActivity.this,NextActivity.class);
startActivity(nextpage);
this.finish();