How to close activity and go back to previous activity in android

后端 未结 18 1784
孤独总比滥情好
孤独总比滥情好 2020-12-07 08:19

I have a main activity, that when I click on a button, starts a new activity, i used the following code to do so:

Intent intent = new Intent(this, SettingsAc         


        
相关标签:
18条回答
  • 2020-12-07 08:30

    When you click your button you can have it call:

    super.onBackPressed();
    
    0 讨论(0)
  • 2020-12-07 08:33

    Finish closes the whole application, this is is something i hate in Android development not finish that is fine but that they do not keep up wit ok syntax they have

    startActivity(intent)
    

    Why not

    closeActivity(intent) ?

    0 讨论(0)
  • 2020-12-07 08:38

    Just don't call finish() on your MainActivity then this eliminates the need to Override your onBackPressed() in your SecondActivity unless you are doing other things in that function. If you feel the "need" for this back button then you can simply call finish() on the SecondActivity and that will take you to your MainActivity as long as you haven't called finish() on it

    0 讨论(0)
  • 2020-12-07 08:39
    Button edit = (Button) view.findViewById(R.id.yourButton);
    edit.setOnClickListener(new OnClickListener() {
    
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(this, YourMainActivity.class);
            startActivity(intent);
            finish();
        }
    });
    
    0 讨论(0)
  • 2020-12-07 08:40

    if you use fragment u should use

    getActivity().onBackPressed();
    

    if you use single activity u can use

    finish();
    
    0 讨论(0)
  • try this code instead of finish:

    onBackPressed();

    0 讨论(0)
提交回复
热议问题