Lets say you have one main activity called "main", and other sub-activities that do some work and then return some data. Each time main gets called it starts up the sub-activities one at a time.
Main:
onCreate...{
Intent i1 = new Intent("com.bla.bla.activity1");
startActivity(i1);
//Get bundles from activity1 and save some variables..
Intent i2 = new Intent("com.bla.bla.activity2");
startActivity(i2);
//Get bundles from activity2 and save some variables..
Intent i3 = new Intent("com.bla.bla.activity3");
startActivity(i2);
//Get bundles from activity1 and save some variables..
}
If I understand correctly, activity1 will be created at first, and then destroyed. Will the code resume and start activity2 or will it just start activity1 again because the main activity was paused and then resumed (assuming we don't add an onResume/onPause etc..)?
Edit:
Lets say these sub-activities return one string each, and each string should be saved in one individual column of an SQL entry at the end of the main activity. The main activity should collect the three strings, save it as en entry and the destroy itself.
For this to work, the main activity has to resume for example at intent "i2" after intent "i1" has done some work and returned the string.
How is this result best achieved?
Like Alex said, you wouldn't want to do that. You want to use the different Intents to call different Activities to create a flow to your program. Main might lead to Activity 1, Activity 1 leads to 2, 3, or 4. If you have several activities, you can use a switch statement to determine which activity you want to launch, based on conditions that are, or are not, met.
Hope this helps.
EDIT Answer:
If you want to save variables from different Activities, the you would want to start the second Activity from the first, the third, from the second, and so on. To create the data for the database, you can simply create an object, and then pass that object from Activity to Activity, using the Parcelable interface. Within each of your Activities you would want to use methods to save the returned data to the database, as you see fit.
Related
I don't know how to implement this situation.
I have an app, during the start of the app, I need to calculate something and then, based on the results, start or Activity1 or Activity2.
How can I achieve this goal?
If I put the calculations in Activity1, and the calculations wants to start Activity2, however Activity1 starts Activity2 and then suddenly finish(), but the screen flickers because there is an activity that starts and ends in a few msecs. This is not very smart.
you should use a splash activity , and put your calculations there , and then based on the result you start the activity you want to
Many ways you can do that. Let me share you one example given as follows
Create New activity ( StartActivity ) which is your root activity.
Within this activity initialize some object like ( App Config, fabric, push notification, share preference data etc.)
Here you can calculate your necessary data and pass using bundle
Create Intent Object for translation to Activity1.class
if(BuildConfig.DEV_ENV && CounterPreference.getInstance().isFirstStart()) {
Intent intent = new Intent(this, Activity1.class);
startActivityForResult(intent, DEBUG_OPTIONS_ACTIVITY);
} else {
StartupManager.getInstance().start(StartActivity.this);
Logger.d(TAG, "Application Started.....*************************");
}
Add your calculation in Application class onCreate() method.And Add your activity decision in Application class.
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.
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.
In my main activity I want to override the onResume method so it acts differently, depending on the activity, which was opened before.
For example: if the user creates a new Item for the listView (in the addNewItem activity), I need to reload the database to display the newItem.
But if he just switches into an activity, which doesn't change anything with the objects displayed in the main activity, the database shouldn't be reloaded and the GUI shouldn't be build again.
Therefor I need to know which activity was 'opened' before.
I hope you understand my problem.
A dirty way is to extend your application class and set an attribute lastActivity that you would set in every onResume/OnPause methods of every activities in your app. This way, when your main activity on resume is called, just read this lastActivity field to know where you come from. But I think it's the dirty way to do it :D
You can send something through in your intent when you call the Activity.
Intent myIntent = new Intent(Activity1.this,Activity2.class);
Bundle myData = new Bundle();
myData.putString("previousActivity", "Activity1");
myIntent.putExtras(myData);
startActivity(myIntent);
Then in the new Activity, you access this and compare the result:
Intent myLocalIntent = getIntent();
Bundle myBundle = myLocalIntent.getExtras();
String str1 = myBundle.getString("previousActivity");
if string.equals("Activity1"){
// do code changes for activity 1 here
}
There are slightly more refined ways of doing this (i.e passing through an int variable which corresponds to a particular Activity) but this is the most basic way.
The right way to do it, is to start every activity with startActivityForResult(intent, resultCode).
When an activity exits, it can call setResult(int) to return data back to its parent.
Link to Developers Resource
I have three activities, Activity1, Activity2, and Activity3.. Activity1 is the main activity from which am switching to other two activities. While I switch to Activity2 from main activity it starts properly, when I switch to Activity3 from main activity and come back to the Activity2 all previous data will be lost and it starts from starting.
Is there any other way to switch to the activities other than using the startActivity() method.
You can store the data in static variable in another class and can call these whenever required.. i think it will work
Hmm, you seem to need to pass information between Activities.
You should use a Bundle to pass information from Activities 1 to 3 to 2,
Or use startActivityForResult(Intent, int) if you're going from Activities 1 to 3 to 1 to 2.
Try this, hope it helps.
Sender.java:
Intent i = new Intent(this, Receiver.class);
i.putExtra("object_name", object);
startActivity(i);
Receiver.java:
Bundle extras = getIntent().getExtras();
Type _variable = extras.getTypeExtra("object_name");
Do note that getTypeExtra(...) requires you to specify the type of information you are about to retrieve.
ex. getStringExtra(...), getFloatExtra(...)