从第3活动到第1活动

时间:2015-04-09 14:58:31

标签: android android-activity

我在这里读到我必须使用startActivityForResult(),但我不知道如何在我的案例中使用它,有人可以帮我整合吗?

所以在MainActivity中,用户登录,从那里开始SecondActivity,用户将看到两个按钮,一个用于第三个活动,另一个用于注销,如果选择了第三个活动,则会向用户显示一个后退按钮和一个注销按钮,现在问题是两个按钮都将返回到第二个活动。

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();
        }

    }

第二项活动

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 个答案:

答案 0 :(得分:0)

尚未在IDE中测试过,但您应该明白这一点:

第二项活动

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();
            }
        }
    }

第三项活动:

   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

答案 1 :(得分:0)

从ThirdActivity.class

启动FirstActivity.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);
}

在FirstActivity.class中添加以下代码,以便在按下硬件按钮的同时不从FirstActivity.class返回到ThirdActivity.class。

  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
        }
    }

要杀死除第一个活动以外的所有活动

Intent intent = new Intent(getApplicationContext(), FirstActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);