Having trouble passing data between activities.
ListActivity is collecting data and when back button is pressed returns to MainActivity and then want to get that data via onResume method but I don't get anything.
How can this problem be solved?
ListActivity.java
#Override
public void finish() {
i = new Intent(ArrayListActivity.this, MainActivity.class);
i.putParcelableArrayListExtra(Constants.TAG_SELECTED_PRODUCT_LIST, selected_list);
super.finish();
}
MainActivity.java
#Override
protected void onResume() {
super.onResume();
Bundle extras = getIntent().getExtras().getBundle(Constants.TAG_SELECTED_PRODUCT_LIST);
if(extras != null) {
selected_list = extras.getParcelableArrayList(Constants.TAG_SELECTED_PRODUCT_LIST);
myListView.setAdapter(new ProductAdapter(MainActivity.this,
R.layout.array_lisviewt_item_row, selected_list));
}
}
You probably want to start your second activity from the first one via the startActivityForResult(...) method.
This method allows you to transport results from a launched activity back to it's launching activity.
From the documentation:
Launch an activity for which you would like a result when it finished.
When this activity exits, your onActivityResult() method will be
called with the given requestCode. Using a negative requestCode is the
same as calling startActivity(Intent) (the activity is not launched as
a sub-activity).
You'll want to start the activity with startActivityForResult() and override onActivityResult() to handle the data you return from the second Activity.
Check out this article on the Android developers site for more info.
Perhaps you should
Call your ListActivity with startActivityForResult(), from your MainActivity.
Once you finished working on the ListActivity, you call setResult() to set the data, followed by finish().
This will bring you back to your previous MainActivity. So how do you retrieve the data set by your ListActivity?
In MainActivity, override onActivityResult().
There's a brief explanation of this mechanism in Starting Activities and Getting Results.
Related
Basically: Activity A calls startActivityForResult, which launches Activity B. At this point, Activity B SHOULD call back to Activity A's onActivityResult method. Instead, Activity B then incorrectly calls back to Activity C's onActivityResult method. Note that Activity B.getCallingActivity() returns Activity A, as expected.
In more detail:
I have three Activities:
EditModuleActivity (Activity A)
EditMotorActivity (Activity B)
ConfigurationActivity (Activity C)
Inside of EditModuleActivity (Activity A), I have this code:
private void launchIntentFromHere(){
int requestCode = 101;
Intent editMotorIntent = new Intent(context, EditMotorActivity.class);
editMotorIntent.putExtra(EditMotorActivity.EDIT_MOTOR_CONTROLLER, item);
editMotorIntent.putExtra("requestCode", requestCode);
setResult(RESULT_OK, editMotorIntent);
startActivityForResult(editMotorIntent, requestCode);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
DbgLog.error("*************************** why is this never getting called?");
}
Inside of EditMotorActivity (Activity B), I have this code:
private void doThisThing(Object item){
// blah blah build intent
DbgLog.error("calling activity: " + getCallingActivity().toString()); // this prints out Activity A, which is what I expect.
returnIntent.putExtra(EDIT_MOTOR_CONTROLLER, item);
setResult(RESULT_OK, returnIntent);
finish();
}
ConfigurationActivity (Activity C) has this in it:
private void launchIntent(Object item){
Intent editMotorIntent = new Intent(context, EditMotorActivity.class);
editMotorIntent.putExtra(EditMotorActivity.EDIT_MOTOR_CONTROLLER, item);
setResult(RESULT_OK, editMotorIntent);
startActivityForResult(editMotorIntent, 1);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// deals properly with the results
}
When I am in EditModuleActivity and I launch EditMotorActivity, ConfigurationActivity.onActivityResult gets called. Instead, When I am in EditModuleActivity and I launch EditMotorActivity, EditModuleActivity.onActivityResult should get called but that's not happening.
Questions
Why is the wrong onActivityResult getting called??
How do I call the right onActivityResult? Is my expectation incorrect? How else does this work?
If the wrong one gets called, how to I call the correct one?
Answers I've seen that didn't help
My code is the same as the accepted answer here: onActivityResult() not being called in activity
I do not have android:launchMode="singleInstance" in my Manifest
I don't know what a "fragment" is but I don't think I have that.
Note that I'm NOT calling two different Activities from one Activity, as in this question: Handling onActivityResult in Android app having more than one activity Instead, I'm calling the SAME activity from two different Activities.
A comment here says "If you call activity B from class A, it will always return codes to the class A That's the same if you call D from C." But I calling Activity B from Activity B and it returns to Activity C. WHY?? WRONG onActivityResult() being called
Like this:
B
/ \
A C
Not this:
X Z
\ /
Y
I was overriding the onStop method like this:
#Override
public void onStop(){
setResult(RESULT_CANCELED, new Intent());
finish();
}
Basically, I didn't understand how the Android Lifecycle worked. I thought onStop() would only be called when an Activity was KILLED, and I thought it wouldn't be killed if it was waiting for a response from another Activity.
So what was happening was this:
Activity C launched Activity A, which launched Activity B. When Activity B started, A.onStop() was called. Then, when Activity B finished, it (correctly) tried to return back to Activity A, but Activity A was already Stopped, so it returned all the way back to Activity C.
OK - most likely you have a reference to the wrong "context" or a static reference to an activity or something weird elsewhere in your code.
To directly answer your question(s):
Your code doesn't explain why - you need to post more code... if possible.
Your expectation is correct. You appear to clearly understand how it startActivityForResult works. YOur references also demonstrate that you know what you are doing. It does not "work" any other way... but that doesn't mean you don't have an error (see "1" above!)
If you are unable to figure out what went wrong, then you can always call the Activity that you want explicitly (startActivity) passing the intent that you want and then processing that intent. You may want to research onNewIntent if you still are getting weird results in rare cases...
If somebody else had a similar problem (when startActivityForResult() returns to wrong activity, not to the one which called it), take a look at this solution
In my android app, I have an activity that displays profile information. Then it opens a new activity to make changes to the activity. After you save changes, it closes the edit activity from the edit activity, then goes back to the profile displaying activity, there I need to restart this activity to refresh the data.
Is there a way I can restart the activity that opened the current activity?
There are three ways you can achieve what you want:
Start the profile activity by calling startActivityForResult() and refresh your data in onActivityResult()
Finish() the activity when start the profile activity and then override onBackPress() in the profile activity to start the previous activity and then call finish().
Override onBackPress() in the profile activity to start the previous activity with the flag Intent.FLAG_ACTIVITY_CLEAR_TOP and refresh your data in onNewIntent()
Use Android Activity Result Pattern that communticates parent and child activities:
In your profile activity (parent):
#Override
public void onClick(View v) {
startActivityForResult(
new Intent(this, NameActivity.class),
1); // 1 to identify caller activity.
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
updateProfile();
}
}
In your editor activity (child):
#Override
public void onClick(View v) {
setResult(
RESULT_OK);
finish();
}
You won't have to "restart" Activity A.
You can call startActivityForResult(Intent, int) to start your new Activity, and then receive extras via the onActivityResult(int, int, Intent) call.
http://developer.android.com/reference/android/app/Activity.html#StartingActivities
Alternatively, look into the onResume() method, which is called every time your screen becomes visible. Assuming you persisted some state in Activity B (e.g. a SharedPreference or a database record), in onResume() you can refresh Activity A.
Activity Display;
Activity Modification;
When you startActivity M in your Activity D,don't D.finish();
then, when you M.finish(),you don't need "restart" Activity D,because it is already there.
You have two choices to "refresh the data":
1.startActivityForResult as they said.I won't explain any more.
2.make a cache.
save data in some static variables or sqlite or even sharedPreference.Then in onResume() of Activity D,get those data and show them.
Try this: If A is calling B and when B ends you need to update A, then in A launch B with startActivityForResult(). When B returns, the method onActivityResult() of A will be called. In this method (of A) you can test, if necessary, that some stored values are changed. At this point you do something like:
finish();
startActivity(intent); //start the activity A
Im working on my level-selector activity right now and I want to get the result from wich level I chose in my MainGameView, that is run by another activity. How would I go about doing this?
This is what Ive for when you choose level 1:
Button test1 = (Button)findViewById(R.id.test1);
test1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
setLevel(1);
Intent start = new Intent("nielsen.happy.activities.HAPPYACTIVITY");
startActivityForResult(start, getLevel());
finish();
}
});
but where do I go from here? How do I get this value into my view so I can change bitmaps and other values depending on what level they chose? Is there something I need to do in my "HappyActivity"(MainGameAcitvity) to get the result? cus right now its returning 0
explenation of how games set up: Menu -> levelselector -> Game. So I need to get result from levelselector into game, so it knows what level to start.
(updated corrected response)
Don't use startActivityForResult().
Try this for Activity A:
Intent start = new Intent("nielsen.happy.activities.HAPPYACTIVITY");
start.putExtra("level", getLevel());
startActivity(start);
finish();
Then in Activity B, do this:
Extras mBundle = getIntent().getExtras();
int level = mBundle.getInt("level");
(original, incorrect response)
Lets call activity A the one that houses your sample code above.
Lets call activity B the one that you refer to as HAPPACTIVITY.
Activity A calls startActivityForResult() with a request code.
Activity B starts up and before exiting, you call setResult(int code).
When activity B finishes, A returns to the top via the method onActivityResult().
Implement an onActivityResult() and see what attributes you get.
FYI there is a condition where setResult(RESULT_OK) or setResult(RESULT_CANCELED) will not trigger onActivityResult() in A (I can't recall what the case is).
I start an activity from the main one using a Intent:
Intent i = new Intent(getApplicationContext(), InfoChiamata.class);
i.putExtra("codice_cliente", codice_cliente[tv_clicked_id]);
i.putExtra("descrizione_chiamata", descrizione_chiamata[tv_clicked_id]);
startActivity(i);
How can I edit the main activity ui from the activity started whit Intent?
How can I know when i return back from the second activity to the main one? I tried to Override the onResume and onStart method but the application doesn't even start. I tryed to override onRestart method when it get called the application crash.
#Override
protected void onRestart() {
if(call_back == 1)
Toast.makeText(getApplicationContext(), "asd", Toast.LENGTH_LONG).show();
}
call_back variable is set to 1 from the secondary activity, when it is launched.
Thanks, Mattia
try startActivityForResult instead which gives you a callback to your first activity. also don't use the application context, unless absolutely necessary, use the activity context instead. Also when you call certain methods from the activity class and override them like onRestart or onStop or onResume you have to do super.onResume() inside the method first, making sure that the app life cycle is not broken.
Try starting your new Activity with
startActivityForResult(i, 1);
Then, in your main activity, use this code to catch when the user leaves the second activity and returns to the first one:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//your request code will be 1 since you started the
//activity with result 1. Your result code will be detemined
//by if the activity ended properly.
}
It sounds like maybe you want to use startActivityForResult(Context, Intent) to call your second activity. When you do this, your second activity can return a value to the activity that called it, and you can decide what to do from there. When the second activity is finished your first activity will receive a callback. This tells your first activity that the second one is finished and it is passed the result of what happened in the second activity.
It is explained in the Android docs here: http://developer.android.com/reference/android/app/Activity.html#startActivityForResult(android.content.Intent,%20int)
My app has 3 classes. First class is a splash screen, second class contains a list of playlists and third class that playlist's content. When a playlist is selected that third class get started to show the playlist content. On second class I have :
#Override
protected void onStop() {
super.onStop();
System.out.println("onStop Playlist!!!!");
}
protected void onDestroy() {
super.onDestroy();
System.out.println("onDestroy Playlist");
}
and when third class is ready to start, I get on DDMS the messages :"onStop Playlist!!!!" and "onDestroy Playlist". Why are this method called? Shouldn't be called only onPause method? The problem is that I want to stop some timer when app is finishing, but I don't know in this case where can I stop the timer. Any idea?
I call the third class like this :
Intent i = new Intent(getBaseContext(), ViewPlaylist.class);
i.putExtra("id", idPlaylist[position]);
i.putExtra("timer", timerPlaylist[position]);
startActivity(i);
finish();
The problem is that I call finish() ?
If you read the docs for the Activity class(for the onDestroy() method) will see that :
The final call you receive before your activity is destroyed. This can
happen either because the activity is finishing (someone called
finish() on it, or because the system is temporarily destroying this
instance of the activity to save space. You can distinguish between
these two scenarios with the isFinishing() method.
Its because you are finishing the second activity with finish(). so instead of use startActivityForResult() and override onActivityResult() in second activity, So in this case your second activity's onPause() will be called, and when you finish() third activity you can back to your second activity's onActivityResult() method()
Try this code...
Intent i = new Intent(getBaseContext(), ViewPlaylist.class);
i.putExtra("id", idPlaylist[position]);
i.putExtra("timer", timerPlaylist[position]);
startActivityForResult(i,RESULT_OK);
Yes, you call finish(). This will finish, thus destroy that activity. Just remove the finish() call. It's only needed if you want to destroy a activity.