Button to go back to MainActivity

↘锁芯ラ 提交于 2019-11-30 03:09:27
zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz

Lets say your main activity is called Main.java.

btnBack.setOnClickListener(new OnClickListener(){

  private void onClick(){
    Intent intent = new Intent(currentActivity.this, Main.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);   
    startActivity(intent);
  }
});

use startActivity(intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));

Sometimes you can just call activity.finish() to end current activity, so the main (first created) activity will pop out.

If this is not your case, do this:

Intent intent = new Intent(getApplicationContext(), Main.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)             
startActivity(intent);
Intent intent = new Intent(this, Main.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);

Well from wherever you are just call startActivity() with the required parameters inside the buttons onClick method. That's it.

public void onBackPressed(){
    finish();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!