0

I have read on here that I have to use startActivityForResult() but I don't know how to exactly use it on my case, Could someone help me integrate it?

So in the MainActivity, the user logs in, from there it will start the SecondActivity and the user would be presented with two buttons, one for a third activity and the other is logging out, if the third activity is selected the user would be presented with a back button and a logout button, now the problem is that both buttons would just be going back to the second activity.

MainActivity.

public void login(View v){
        EditText uname_field = (EditText) findViewById(R.id.uname_field);
        EditText pword_field = (EditText) findViewById(R.id.pword_field);

        username = uname_field.getText().toString();
        password = pword_field.getText().toString();
        if(username.equalsIgnoreCase("admin") && password.equalsIgnoreCase("admin")){
                Intent intent = new Intent(this, SecondActivity.class);
                startActivity(intent);
        }else {
            Toast.makeText(context, "Access Denied", Toast.LENGTH_SHORT).show();
        }

    }

Second Activity

public void logout(View v) {
               finish();
    }

    public void viewProfile(View v){
        Intent intent = new Intent(this, ThirdActivity.class);
        startActivity(intent);
    }

Thirdactivity

public void logout(View v) {

        finish();

    }



    public void back(View v){

        finish();
    }

2 Answers2

1

Just start FirstActivity.class from your ThirdActivity.class

public void logout(View v) {    
    Intent intent = new Intent(this, FirstActivity.class);
    intent.putExtra("lastActivity", "third");
    startActivity(intent);
}

public void back(View v){

    Intent intent = new Intent(this, FirstActivity.class);
    intent.putExtra("lastActivity", "third");
    startActivity(intent);
}

And add below code in FirstActivity.class for not going back to ThirdActivity.class from your FirstActivity.class while pressing back hardware button.

  public void onBackPressed() {
        if(getIntent().getExtras().getString("lastActivity").equals("third")){
           //from third activity
        } else{
           //from other activity, here you can exit or can do other things
        }
    }

To kill all activity except the first activity use

Intent intent = new Intent(getApplicationContext(), FirstActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Md. Nasir Uddin Bhuiyan
  • 1,598
  • 1
  • 14
  • 24
  • and if user will press back in FirstActivity he will be again in Third activity (backstack) – Mickey Tin Apr 09 '15 at 15:18
  • that's a hardcode fix actually, how do we know who started FirstActivty ? the 3rd or maybe it's really started by user from home screen and then back button will be blocked unnecessarily ? – Mickey Tin Apr 09 '15 at 15:31
  • The developer must use logic according to his/her requirements. But I have edited my answer for getting acknowledgement for which activity was last time active. – Md. Nasir Uddin Bhuiyan Apr 09 '15 at 15:41
  • it does not fix anything, the backstack of activities remains in memory, what if user will repeat First->Second->Third->First->Second->Third ..... OutOfMemoryException eventually will happen – Mickey Tin Apr 09 '15 at 16:32
  • I edited my answer. I hope the developer will understand and apply this according to his/her requirements. Thank You @MickeyTin – Md. Nasir Uddin Bhuiyan Apr 09 '15 at 16:44
0

Haven't tested it in IDE, but you shoud get the idea :

Second Activity

static final int THIRD_ACTIVITY_REQUEST = 1;

    public void logout(View v) {
                   finish();
        }

        public void viewProfile(View v){
            Intent intent = new Intent(this, ThirdActivity.class);
            startActivityForResult(intent,THIRD_ACTIVITY_REQUEST );
        }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == THIRD_ACTIVITY_REQUEST ) {
            if (data != null && data.hasExtra("userLoggedOut")) {
                finish();
            }
        }
    }

Third Activity :

   public void logout(View v) {

       Intent result = new Intent();
       result.putExtra("userLoggedOut", true);
       setResult(RESULT_OK, result);
       finish();

  }



        public void back(View v){

            finish();
        }

https://developer.android.com/training/basics/intents/result.html

Sending data back to the Main Activity in android

Community
  • 1
  • 1
Mickey Tin
  • 3,408
  • 10
  • 42
  • 71