我想检查调用活动的名称以检查某些条件。解决方案是什么?
答案 0 :(得分:6)
我认为你甚至可以实现这一目标
getCallingActivity().getClassName()
答案 1 :(得分:4)
您可以使用Bundle
看一下这个例子: http://www.balistupa.com/blog/2009/08/passing-data-or-parameter-to-another-activity-android/
基本上你需要把调用者的名字作为参数:
Bundle bundle = new Bundle();
bundle.putString(this.class.getName(), “ClassName”);
Intent newIntent = new Intent(this.getApplicationContext(), ActivityClass2.class);
newIntent.putExtras(bundle);
startActivityForResult(newIntent, 0);
在ActivityClass2中,您可以使用以下方法读取此参数:
Bundle bundle = this.getIntent().getExtras();
String className = bundle.getString(“ClassName″);
答案 2 :(得分:1)
步骤1: 创建你的界面 - >右键你的项目 - > New-> JavaClass-> Kind = Interface
public interface ActivityConstants {
public static final int NameFromTheFirstActivity = 1001;
public static final int NameFromTheSecondActivity = 1002;
public static final int NameFromTheThirdActivity = 1003;
}
步骤2: 进入每个活动(Activity1或Activity2或Activity3)当你建立你的意图调用ActivityFromWhereYouWantChooseTheAction(称为ActivityChooseAction)时,你需要添加一个额外的... 进入Activity1写这个:
Intent intent = new Intent(getApplicationContext(), ActivityChooseAction.class);
intent.putExtra("calling-activity", ActivityConstants.NameFromTheFirstActivity);
startActivity(intent);
进入Activity2写下这个:
Intent intent = new Intent(getApplicationContext(), ActivityChooseAction.class);
intent.putExtra("calling-activity", ActivityConstants.NameFromTheSecondActivity);
startActivity(intent);
进入Activity3写下这个:
Intent intent = new Intent(getApplicationContext(), ActivityChooseAction.class);
intent.putExtra("calling-activity", ActivityConstants.NameFromTheThirdActivity);
startActivity(intent);
进入ActivityChooseAction后写下:
Public class ActivityChooseAction...{
String parametersharedtouseinthisactivity="";
...
...
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activitychooseaction_layout);
mPreferences = getSharedPreferences("CurrentUser", MODE_PRIVATE);
int callingActivity = getIntent().getIntExtra("calling-activity", 0);
switch (callingActivity) {
case ActivityConstants.NameFromTheFirstActivity:
//write your parameter....if u have saved it into preferences you can do like
parametersharedtouseinthisactivity = mPreferences.getString("nameparameter", defaultvalueyouwant);
break;
case ActivityConstants.NameFromTheSecondActivity:
parametersharedtouseinthisactivity = mPreferences.getString("nameparameter", defaultvalueyouwant);
break;
case ActivityConstants.NameFromTheThridActivity:
parametersharedtouseinthisactivity = mPreferences.getString("nameparameter", defaultvalueyouwant);
break;
}
}
我希望这可以帮助别人,我按照用户在这里说的话:how to know the calling activity in android