[英]How to know which activity clicked the button?
I have 10 activites that extends baseActivity. 我有10个扩展baseActivity的活动。 My BaseActivity basically has a help menu icon.
我的BaseActivity基本上有一个帮助菜单图标。 When the user press the help menu Icon, I should display the help menu for that activity.
当用户按下帮助菜单图标时,我应该显示该活动的帮助菜单。 How can I know which activity called the help menu so I know which xml resource to display
我如何知道哪个活动称为帮助菜单,所以我知道要显示哪个xml资源
public class BaseActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.help_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.help_menu_item:
displayHelpMenu();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
protected void displayHelpMenu(){
// I will load the xml to the textview and display it in dialog. How can I know which xml resource to load
}
}
One way to find out the Activity
is to use instanceOf
. 找出
Activity
一种方法是使用instanceOf
。 Something like this: 像这样:
protected void displayHelpMenu() {
// I will load the xml to the textview and display it in dialog.
// How can I know which xml resource to load
if(this instanceOf DerivedActivity.class) {
// load XML file
}
else if(...) {
...
}
}
However, in my opinion, while this solves your problem, this is not the correct way of doing things. 但是,我认为,尽管这可以解决您的问题,但这并不是正确的处理方式。 In OOP when you end up with a situation like this, you should make use of method overriding .
在OOP中,当您遇到这样的情况时,应使用方法重写 。
The base class
defines general functionality of loading the resource, while the derived class should provide the name of the file to load. base class
定义了加载资源的一般功能,而派生类应提供要加载的文件的名称。 In other words, the base class
should not know about the derived class
. 换句话说,
base class
不应该知道derived class
。 A better solution would be: 更好的解决方案是:
protected void displayHelpMenu() {
// ask each child class for the help menu resource file
int resId = getHelpMenuResource();
// write code to load XML file with resId
...
...
}
protected int getHelpMenuResource() {
return defaultMenuId;
}
Now in your derived class, @Override
the getHelpMenuResource()
method and return resource id specific to that activity. 现在在派生类中,
@Override
getHelpMenuResource()
方法并返回特定于该活动的资源ID。
how about make your BaseActivity abstract and add a getMenuResource method? 如何使您的BaseActivity抽象并添加getMenuResource方法?
public abstract class BaseActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.help_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.help_menu_item:
displayHelpMenu();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
protected void displayHelpMenu(){
// I will load the xml to the textview and display it in dialog. How can I know which xml resource to load
int menuResourceId = getMenuResourceId();
// do the rest
}
public abstract int getMenuResourceId();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.