0

I have 2 main Activities, login and list, which uses firebase auth. after few times of login and logout, if I press the back button (happens on both activities) the same activity reloads (although I use finish() when intenting between them).

i tried to override onBackButtonPressed, but nothing worked. I printed messages in onCreate and onDestroy to make sure they are called.

Login Activity (only relevant pieces)

public class LoginActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        Log.wtf("TESTING", "CREATED: " + getClass().getSimpleName() + " -- TASK ID: " + getTaskId());

        setAllOutlets();
        mAuth = FirebaseAuth.getInstance();



    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.wtf("TESTING", "DESTROYED: " + getClass().getSimpleName() + " -- TASK ID: " + getTaskId());
    }


    private void goToListActivity(){

        Intent listActivityIntent = new Intent(LoginActivity.this, com.technion.android.mylists.ListActivity.class);
        startActivity(listActivityIntent);
        finish();

    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }
}

List Activity is about the same, ill add it if someone will say it's needed, because its pretty big... i use auth state listener and made sure Im calling finish when user clicks log out button

for both activities I would like to press the back button and minimize the app, regardless of what activity the user is in.

2 Answers 2

1

Try this,

@Override
public void onBackPressed() {
     super.onBackPressed();
     finish();
     moveTaskToBack(true);
}

https://developer.android.com/reference/android/app/Activity.html#moveTaskToBack(boolean)

4
  • 2
    Please add some explanation to your answer
    – mechnicov
    Commented Apr 2, 2019 at 14:37
  • Im also intrested to know why this is happaning, does moveTaskToBack has other side effects? will the problem go away if ill move task to back or will it just hide the symptoms? Commented Apr 2, 2019 at 15:13
  • Move the task containing this activity to the back of the activity stack. The activity's order within the task is unchanged. If false then this only works if the activity is the root of a task; if true it will work for any activity in a task. If the task was moved (or it was already at the back) true is returned, else false. developer.android.com/reference/android/app/…
    – smtylmz
    Commented Apr 2, 2019 at 17:31
  • thank, I solved the problem with this answer eventually, seems bit "unclean" but it got the job done Commented Apr 3, 2019 at 8:44
0

As I remember, you don't need to call finish() if you are overloading OnBackPressed()

In this case it's reasonable

@Override
public void onBackPressed() {
    switch (behaviorOptionsGroup.getCheckedRadioButtonId()) {
        case R.id.finish:
            finish();
            return;
        case R.id.moveTaskToBack:
            moveTaskToBack(false);
            return;
        default:
            super.onBackPressed();
    }
}

In your case, i guess, you call finish() twice. Once in super.onBackPressed(); and second time explicitly then you call it after.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.