gpt4 book ai didi

android - 如何在不同的 Activity 调用上执行不同的功能?

转载 作者:行者123 更新时间:2023-11-29 20:49:00 25 4
gpt4 key购买 nike

假设我有三个类(class):第一、第二和第三。

我实际上想做的是:

如果我从 First 调用第二个 Activity ,那么我希望执行一些 task1,如果我从 Third 调用第二个 Activity ,那么应该执行一些不同的 task2。

我怎样才能做到这一点?

最佳答案

在开始Second的intent中,放入一些Bundle数据来标记这个intent是来自First还是Third。详细信息是 here .

如果我这样做,

// Constants.java
public static class Constants {
public static String BUNDLE_KEY_FROM = "_BUNDLE_KEY_FROM";
}

FirstThird 中,

Intent intent = new Intent(this, Second.class);
intent.putExtra(Constants.BUNDLE_KEY_FROM, "First"); // or "Third"
startActivity(intent);

然后在Second.onCreate()中,

Bundle extras = getIntent().getExtras();
if (extras != null) {
// get data via the key
String val = extras.getString(Constants.BUNDLE_KEY_FROM);
if (val.equals("First") ) {
funcFirst();
}else if(val.equals("Third") ){
funcThird();
}
}

关于android - 如何在不同的 Activity 调用上执行不同的功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29679903/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com