Interaction between activities - android

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)

Related

Android back stack with multiple Activities

I have a few activities and can't figure out a way to make it work with the backstack.
Is there a way to do this:
MainActivity ->(intent) subActivity ->(intent)subsubActivity->(back press)subActivity->*(back press)MainActivity
*This is where I am having problems. Since I am coming from my subsubActivity, even thought I used android:noHistory="true" in the manifest, it doesn't go back to main activity.
Thanks for any help!
This is a perfect candidate for using a combination of startActivityForResult and overriding onActivityResult in the calling Activity.
Assuming you have Activity A, which starts Activity B (which we cannot move back to), which starts Activity C:
Activity A will call Activity B the way you are now. Activity B, however, will call startActivityForResult(Intent, int) instead of just startActivity(Intent). This way, when we return from Activity C, we can call finish() on Activity B and return to Activity A, like so:
public class ActivityB extends Activity {
private static final int REQUEST_CODE_ACTIVITY_C = 1001;
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if(requestCode == REQUEST_CODE_ACTIVITY_C) finish(); // If coming back from Activity C, finish()
}
private void openActivityC(){
Intent intent = new Intent(this, ActivityC.class);
startActivityForResult(intent, REQUEST_CODE_ACTIVITY_C);
}
}
Now when you call openActivityC(), you're ensuring that onActivityResult() will be called when returning from Activity C, thus allowing you to end the Activity and return back to Activity A.
You can provide even more specific actions (such as setting/checking the result code (e.g. if it canceled (Activity.RESULT_CANCELED) or successful (Activity.RESULT_OK)) for statuses, to better determine what that calling Activity should do.
Hopefully that helps explain it a bit.
Edit: Also an afterthought, if there's no chance you'll ever want to go back to Activity B, then #Kay 's solution of just calling finish() after firing the Intent for Activity C would be the simplest approach.
If i get what you want You can use startActivities with an array of intents which will be fired one after another activity1->activity2->activity3 -back-activity2 -back-acitivty1

onActivityResult in wrong activity is being called

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

How to restart previous activity in android?

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

Send data to activity which is running in background

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.

Have a main Activity that calls another Activity

My Activity calls another Activity during which , the app should move to other activity and should not affect the main Activity.Should the main activity be onPause() ?
and after the second Activity comes back to the Main Activity should it have OnResume()?
if you have any example will be great.
thanks for helping!!!
No rather you should use startActivityforResult(intent, requestCode) to start the next activity
and then override
protected void onActivityResult( final int requestCode,
final int resultCode,
final Intent data)
See Returning a Result from a Screen
That way you will know which activity was called before returning to main
If you are using Intents, there is no need for pause and resume, provided the flow of your activity is fine. Suppose you are going from A to B, all you do is mention the current activity name and the next activity where it should go.
For Ex:
Intent i = new Intent(ClassA.this,ClassB.class); where you are in class A and you are going to classB

Categories

Resources