pindol
pindol

Reputation: 2110

Interaction between activities

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

Upvotes: 0

Views: 1873

Answers (3)

Mel Stanley
Mel Stanley

Reputation: 374

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)

Upvotes: 1

coder
coder

Reputation: 10520

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. 

}

Upvotes: 2

L7ColWinters
L7ColWinters

Reputation: 1362

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.

Upvotes: 8

Related Questions