Android 确定单击哪个按钮来启动活动

发布于 2024-11-04 19:27:19 字数 562 浏览 0 评论 0原文

我有一个将从父活动启动的活动,但子活动的行为将根据在父活动中单击按钮来确定。

我一直在尝试确定调用哪个 button.onClick 方法来启动子活动,但是可惜,我失败了。

具体来说,我一直专注于使用 ComponentName 并将其展平为字符串,但每次尝试执行此操作时,我都会收到空指针异常。

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.subactivity);
        ComponentName callingActivity =  SubActivity.this.getCallingActivity();
            TextView listtype = (TextView) findViewById(R.id.subactivity_listtype);
        listtype.setText(callingActivity.flattenToString());

I have an activity that will be started from a parent activity, but the behavior of the subactivity will be determined based upon with button is clicked in the parent activity.

I have been trying to determine which button.onClick method was called to start the subactivity, but alas, I have failed.

Specifically, I have been focused on using the ComponentName and flattening it to a string, but every time I attempt to do this, I get a Null Pointer Exception.

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.subactivity);
        ComponentName callingActivity =  SubActivity.this.getCallingActivity();
            TextView listtype = (TextView) findViewById(R.id.subactivity_listtype);
        listtype.setText(callingActivity.flattenToString());

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

念﹏祤嫣 2024-11-11 19:27:19

您需要作为 Extras 传递一个自定义值,该值将告诉您哪个按钮启动了该活动。这必须在调用活动中完成,而不是在新活动中完成。

这是一个示例,可以帮助您

第一个上下文(可以是 Activity/Service 等)

您有几个选项:

1) 使用 Bundle 来自 Intent

Intent mIntent = new Intent(this, Example.class);
Bundle extras = mIntent.getExtras();
extras.putString(key, value);  

2) 创建一个新的 Bundle

Intent mIntent = new Intent(this, Example.class);
Bundle mBundle = new Bundle();
mBundle.extras.putString(key, value);
mIntent.putExtras(mBundle);

3) 使用 putExtra() Intent 的快捷方法

Intent mIntent = new Intent(this, Example.class);
mIntent.putExtra(key, value);

新建上下文(可以是 Activity/Service 等)

Intent myIntent = getIntent(); // this getter is just for example purpose, can differ
if (myIntent !=null && myIntent.getExtras()!=null)
     String value = myIntent.getExtras().getString(key);
}

注意: Bundles 对所有基本类型、Parcelables 和 Serializeds 都有“get”和“put”方法。我只是将字符串用于演示目的。

You need to pass as Extras a custom value that will tell you which button started the activity. This must be done in the calling activity not the new one.

Here is a sample that can help you

First Context (can be Activity/Service etc)

You have a few options:

1) Use the Bundle from the Intent:

Intent mIntent = new Intent(this, Example.class);
Bundle extras = mIntent.getExtras();
extras.putString(key, value);  

2) Create a new Bundle

Intent mIntent = new Intent(this, Example.class);
Bundle mBundle = new Bundle();
mBundle.extras.putString(key, value);
mIntent.putExtras(mBundle);

3) Use the putExtra() shortcut method of the Intent

Intent mIntent = new Intent(this, Example.class);
mIntent.putExtra(key, value);

New Context (can be Activity/Service etc)

Intent myIntent = getIntent(); // this getter is just for example purpose, can differ
if (myIntent !=null && myIntent.getExtras()!=null)
     String value = myIntent.getExtras().getString(key);
}

NOTE: Bundles have "get" and "put" methods for all the primitive types, Parcelables, and Serializables. I just used Strings for demonstrational purposes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文