获取意图类加载器.我想在secondActivity中将textView中的textView设置为firstActivity的名称(实际上我需要类加载器的名称) [英] Get intent Class loader . I want to set text a textView in secondActivity to name of firstActivity (Actually I need class loader name)
问题描述
在从一种活动转到另一种活动的意图中,如何获得类加载器的名称? 我需要类加载器的名称. 如果我的意图不明确,请查看以下代码.
In going from an activity to another one by an intent, How can I get class loader name ? I need name of class loader. If my intention wasn't clear, Please look at this codes.
我为此问题创建了一个测试应用.它具有三个活动. ActivityA,ActivityB和ActivityC.我在ActivityA和ActivityB中都有一个启动activityC的按钮.
I created a test app for this Question. It has three activity. ActivityA , ActivityB and ActivityC. I have a button in ActivityA and ActivityB that start activityC.
ActivityA:
ActivityA:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_a);
Button btnGoFromActivityAToActivityC = (Button) findViewById(R.id.btnGoFromActivityAToActivityC);
btnGoFromActivityAToActivityC.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(ActivityA.this, ActivityC.class);
startActivity(intent);
}
});
}
活动B:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_b);
Button btnGoFromActivityBToActivityC = (Button) findViewById(R.id.btnGoFromActivityBToActivityC);
btnGoFromActivityBToActivityC.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(ActivityB.this, ActivityC.class);
startActivity(intent);
}
});
}
ActivityC:
ActivityC:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_c);
TextView textViewNameOfFirstActivity = (TextView) findViewById(R.id.textViewNameOfFirstActivity);
if (/* if we came from ActivityA */)
{
textViewNameOfFirstActivity.setText("You came from ActivityA");
}
else if (/* if we came from ActivityB */)
{
textViewNameOfFirstActivity.setText("You came from ActivityB");
}
}
看看ActivityC ...
Look at ActivityC...
我该怎么做?
推荐答案
ClassLoader
不会告诉您Activity
加载了什么内容.它用于加载类.
The ClassLoader
isn't going to tell you what loaded your Activity
; it's for loading Classes.
一种更好的方法是将某种信息发送到Activity C
,告诉它谁打开了它.例如
A better approach would be to send some kind of info to Activity C
that tells it who opened it. e.g.
活动A:
Intent intent = new Intent(ActivityA.this, ActivityC.class);
intent.putExtra("from", "Activity A");
startActivity(intent);
然后在Activity C
的onCreate(Bundle)
方法中,您将获得该值:
And then in your onCreate(Bundle)
method of Activity C
you would get that value:
@Override
public void onCreate(Bundle savedInstance){
....
String fromActivity = getIntent().getStringExtra("from");
textViewNameOfFirstActivity.setText(fromActivity);
}
这篇关于获取意图类加载器.我想在secondActivity中将textView中的textView设置为firstActivity的名称(实际上我需要类加载器的名称)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!