Question

Hello here is my MainActivity:

public class MainActivity extends ActionBarActivity implements android.view.View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button button1 = (Button) findViewById(R.id.button1);
    button1.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    Log.i("clicks","You Clicked B1");
    Intent i=new Intent(MainActivity.this, loginPage.class);
    startActivity(i);

}

}

I would like to know how to go back to this MainActivity when my loginPage has been pressed by the back event.

Here is the loginPage :

public class loginPage extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.connexion);
}
@Override
public void onBackPressed()
{
    finish();
}

}

I don't know what to put in onBackPressed..

Was it helpful?

Solution

By default, Android OS call onBaskPressed() when the back button is pressed and call finish() for the current activity.
Then, the system automatically redirects you to the previous Activity (only if you didn't call finish() in the previous one). So, you don't need to override this method because the OS does it for you.

If press the hardware back button on your emulator doesn't do anything, edit the emulator and make sure that you enable the Hardware keyboard present by checking it. Also, don't use No skin.

AVD edition

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top