Question

Basically my MainActivity has a button which will become invisible after clicking and the SecondActivity will be called after a few seconds. However, when I press the back botton, the button on the MainActivity is still invisible. I want the MainActivity to restart/initialize. I knew it is something to do with onResume or onRestart but how can I implement these methods? Can anyone give me an example? Thanks.

Was it helpful?

Solution

You could call finish() on your MainActivity when you go to your second one. Then Override onBackPressed() in your SecondActivity and start the MainActivity again.

@Override
public void onBackPressed()
{
    // create Intent and start MainActivity again
}

OTHER TIPS

I think you are looking for startActivityForResult. You can see a sample of usage in the Android documentation or here on SO.

Basically in your first activity you overwrite the method onActivityResult and in it (if the result is OK) re-show the button. Then, in the second activity, set the result to be returned to be OK and just finish it however you like (either by pressing the back button or by calling finish()).

Alternatively:

You may ovewrite the onResume method in the first activity and just show the button every time this method is called (note that onResume is called even on activity's first start, but since the button is already shown in your case - it won't have any effect).

@Override
public void onResume(){
  Button b = (Button) findViewById(R.id.myButton);
  b.setVisibility(View.VISIBLE);
}

Inside your Activity, simply write

@Override
public void onResume(){
    // put your code here...
   yourButtonInstance.setVisibility(View.VISIBLE) 
}

and put the logic you need to change the visibility inside it

You could set your button as attribute of the activity and make your button visible in the onPause() or in the onResume() method.

  button.setVisibility(View.VISIBLE);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top