导航至 android 中的 java 代码中的父 activity
Navigate to parent activity in java code in android
我在我的代码中创建了一个适配器视图和底部导航视图。
这是我用于所有导航视图的父级 activity
<activity android:name=".activity.patient.painpoint.PainPointBackView"
android:parentActivityName=".activity.patient.assessment.PatientAssessmentActivity" />
这是我作为所有选项卡的 parentActivity 提供的适配器视图
[![适配器视图][1]][1]
这是我使用的标签之一,
[![选项卡视图][2]][2]
如何导航到父级 activity,即当我从选项卡视图中单击 "assessment" 图标时的适配器视图。如果我使用 onbackpressed() 它会转到另一个选项卡。我不需要调用 using intent,因为它是一个适配器,我需要再次传递所有值。有什么方法可以从 java 代码 (PatientAssessmentActivity.class) 调用父 activity,如果我按工具栏后退按钮,它会导航到父 activity。我需要在单击评估选项卡图标时发生这种情况。
这是我用过的代码,
private void setupNavigationView() {
BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);
Menu menu = bottomNavigationView.getMenu();
MenuItem menuItem = menu.getItem(2);
menuItem.setChecked(true);
assessment = realm.where(Assessment.class).equalTo("patient_id", patientID).findFirst();
Log.v(Constants.TAG, "addsdsds " + assessment);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.action_assessment:
**here i need to navigate to parent activity**
break;
case R.id.action_front:
Intent intent = new Intent(PainPointBackView.this, PainPointFrontView.class);
intent.putExtra("patient_id", assessment.getPatient_id());
intent.putExtra("assessment_id", assessment.getId());
startActivity(intent);
break;
case R.id.action_back:
break;
case R.id.action_left:
Intent intent2 = new Intent(PainPointBackView.this, PainPointLeftView.class);
intent2.putExtra("patient_id", assessment.getPatient_id());
intent2.putExtra("assessment_id", assessment.getId());
startActivity(intent2);
break;
case R.id.action_right:
Intent intent3 = new Intent(PainPointBackView.this, PainPointRightView.class);
intent3.putExtra("patient_id", assessment.getPatient_id());
intent3.putExtra("assessment_id", assessment.getId());
startActivity(intent3);
break;
}
return false;
}
});
}
您可以尝试使用,
Intent intent = new Intent(getApplicationContext(), PatientAssessmentActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
根据 Android 文档,
FLAG_ACTIVITY_CLEAR_TOP If set, and the activity being launched is
already running in the current task, then instead of launching a new
instance of that activity, all of the other activities on top of it
will be closed and this Intent will be delivered to the (now on top)
old activity as a new Intent
参考:Intent
我在我的代码中创建了一个适配器视图和底部导航视图。 这是我用于所有导航视图的父级 activity
<activity android:name=".activity.patient.painpoint.PainPointBackView"
android:parentActivityName=".activity.patient.assessment.PatientAssessmentActivity" />
这是我作为所有选项卡的 parentActivity 提供的适配器视图
[![适配器视图][1]][1]
这是我使用的标签之一,
[![选项卡视图][2]][2]
如何导航到父级 activity,即当我从选项卡视图中单击 "assessment" 图标时的适配器视图。如果我使用 onbackpressed() 它会转到另一个选项卡。我不需要调用 using intent,因为它是一个适配器,我需要再次传递所有值。有什么方法可以从 java 代码 (PatientAssessmentActivity.class) 调用父 activity,如果我按工具栏后退按钮,它会导航到父 activity。我需要在单击评估选项卡图标时发生这种情况。
这是我用过的代码,
private void setupNavigationView() {
BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);
Menu menu = bottomNavigationView.getMenu();
MenuItem menuItem = menu.getItem(2);
menuItem.setChecked(true);
assessment = realm.where(Assessment.class).equalTo("patient_id", patientID).findFirst();
Log.v(Constants.TAG, "addsdsds " + assessment);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.action_assessment:
**here i need to navigate to parent activity**
break;
case R.id.action_front:
Intent intent = new Intent(PainPointBackView.this, PainPointFrontView.class);
intent.putExtra("patient_id", assessment.getPatient_id());
intent.putExtra("assessment_id", assessment.getId());
startActivity(intent);
break;
case R.id.action_back:
break;
case R.id.action_left:
Intent intent2 = new Intent(PainPointBackView.this, PainPointLeftView.class);
intent2.putExtra("patient_id", assessment.getPatient_id());
intent2.putExtra("assessment_id", assessment.getId());
startActivity(intent2);
break;
case R.id.action_right:
Intent intent3 = new Intent(PainPointBackView.this, PainPointRightView.class);
intent3.putExtra("patient_id", assessment.getPatient_id());
intent3.putExtra("assessment_id", assessment.getId());
startActivity(intent3);
break;
}
return false;
}
});
}
您可以尝试使用,
Intent intent = new Intent(getApplicationContext(), PatientAssessmentActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
根据 Android 文档,
FLAG_ACTIVITY_CLEAR_TOP If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent
参考:Intent