如何获得之前的Activty?

时间:2017-11-18 12:10:50

标签: java android

我们在活动A中,我们按照以下代码执行此操作:

if (previousActivity){  // what method calling the previous activity ?
                        Intent intent = new Intent(
                                LoginActivity.this,
                                CmdEndActivity.class);   // I want to go in CmdEndActivity
                        intent.putExtra("username", user);
                        intent.putExtra("useremail", email);
                        startActivity(intent);
                        finish();

                        overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);

                    }
                    else {
                        Intent intent = new Intent(
                                LoginActivity.this,
                                Reservation4.class);  // I want to go in Reservation4
                        intent.putExtra("username", user);
                        intent.putExtra("useremail", email);
                        startActivity(intent);
                        finish();

                        overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);

                    }

有可能吗?

2 个答案:

答案 0 :(得分:0)

试试这个

@Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if (item.getItemId() == android.R.id.home) {
            onBackPressed();
            return true;
        }
        return false;

    }

答案 1 :(得分:0)

您可以使用putExtra中的Intent将活动名称传递给另一个活动名称。然后在目标活动中使用getIntent并检查它是否等于Activity 1Activity 2。如下所示:

Intent intent = new Intent(this, Activity2.class);
intent.putExtra("activity_name", "Activity 1");
startActivity(intent);

在活动2中检查额外值:

String activity_name = getIntent().getStringExtra("activity_name");
if(activity_name.equal("Activity 1")){
  //... goto Activity B
}else if(activity_name.equal("Activity 2")){
  // ... goto Activity C
}