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
Related
I have a project that has A ListView and some Buttons, and some ArrayList and String.
I fill out the ListView from a DB Query, that takes some seconds to Load, and when its loads,end-user do something and go to next Activity Via StartActivity(myIntent) method.
When goes to next activity, it will be back to this activity and cause it has a DB Query, takes some seconds and even if network got problem, it get force close Message.
How can i save the whole instance of this Activity just once time and again Restore it in second time?
Im new to android, any help will appreciate.
This is my onCreate Method :
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.type_of_dairy);
// all methods, views, initials and ....
}
In case your use-case is that you need to start the first activity from the second activity using startActivity method. You can do the following to bring the existing first activity to front:
Intent intent = new Intent(this, FirstACtivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
//intent. // Your code here
startActivity(intent);
Basically this flag "FLAG_ACTIVITY_REORDER_TO_FRONT" will bring the existing activity to top. So, your onCreate() method will not be called again and your issue will be resolved. In case you want to execute some code when this happens, you can use onNewIntent() method of the activity to handle the new intent and initialize your variables/fields there. Let me know if you have any doubts.
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.
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.
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(...)
I have two activities in which I need to pass an object array between them. From the main activity, I start the secondary activity with startActivityForResult(), and pass the array as a ParcelableArray in a Bundle. This works fine to get the array to the secondary activity; I can pull the array out of the Bundle and manipulate it from there.
The problem I'm having is when I want to pass the array back to the calling activity. I need it to occur when a user presses the back/return button on the device. I've tried placing the array back into a Bundle, calling setResult() from within onPause() on the secondary activity, but I'm assuming this is the incorrect location to perform the task. I've reached that conclusion as getCallingActivity() within onPause() returns null.
What's the proper way to return a Bundle to the calling activity in this situation?
Main Activity:
Bundle b = new Bundle();
b.putParcelableArray("com.whatever.data", items);
Intent folderView = new Intent(MainView.this, FolderView.class);
folderView.putExtras(b);
startActivityForResult(folderView, 1);
Secondary Activity:
protected void onPause () {
super.onPause();
Intent result = new Intent();
Bundle b = new Bundle();
b.putParcelableArray("com.whatever.data", items);
result.putExtras(b);
setResult(Activity.RESULT_OK, result);
}
IIRC, I've hooked onKeyDown() and watched for KeyEvent.BUTTON_BACK and called setResult() there. In Android 2.0, you'd hook onBackPressed() instead.
Another option (and one I use frequently when using activities) is the finish() method
Have you tried onDestroy instead of onPause? Alternatively, you could repeatedly call setResult within the second activity whenever the array is updated, as the result won't be passed back until the second activity is actually finished.
There's probably a better solution than either of those, but that's what came to mind.