How to run code when coming back to activity


Ask by : Radiak November 05, 2012 10:38

in my application I have 3 activities. First one -the main from which I start activity #2. From #2 I start #3 and at the same time I finish #2. When I finish #3 I automatically come back to #1. Question: How can I add/run code when coming back from 3 to 1?

not sure if it makes sense. But what I want to do is, when ending #3 and coming back to #1 I want to check if file xyz exists and based on it to change UI in activity #1.

OnResume in #1 is not ran, never. (Probably system doesn't run onpause for this first activity)

If there was only activity #1 and 2 I could use startActivityForResult. But two activities don't do what I need...

View original question

Answer by : Arvind KanjariyaNovember 05, 2012 10:38

See below example.

your activity#1 code is like this

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

}

@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();

          //Do your code here
}
}

Your activity#3 code is like this

public class Activity3 extends Activity{



@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity3); 
}
@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    super.onBackPressed();
    finish();
}


}
View original answer