gpt4 book ai didi

android - 检查是否在我的应用程序中调用了 Activity

转载 作者:行者123 更新时间:2023-11-30 04:34:59 24 4
gpt4 key购买 nike

如何检查我的应用程序中是否调用了 Activity ?

我的问题是,如果用户来自另一个应用程序并且我的应用程序仍在后台,我只想更新数据。

谢谢!

最佳答案

一个可能的想法是反转该过程,即确定 Activity 是否由您的应用程序启动。

假设您有一个 Activity A,您希望在其中的 onResume() 中了解您是否已从您的另一个 Activity 中返回。您应该能够使用 startActivityWithResult() 启动所有其他 Activity ,从 A 传出,并在 onActivityResult() 中检查结果。由于 onActivityResult() 在 onResume() 之前被调用,所以你可以在 onResume() 中设置一个 bool 字段来检查。

所以,代码是:

private static final int RANDOM_ACTION_CODE= 42;

private Button fButton;

private boolean returning= false;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

fButton= (Button) findViewById(R.id.button1);
fButton.setOnClickListener(this);
}

@Override
public void onClick(View v) {
Intent i= new Intent(this, SecondActivity.class);
startActivityForResult(i, RANDOM_ACTION_CODE);
}

@Override
protected void onResume() {
super.onResume();
Toast.makeText(this, "returning = " + returning, Toast.LENGTH_LONG).show();
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RANDOM_ACTION_CODE)
returning= true;
else
returning= false;
}

关于android - 检查是否在我的应用程序中调用了 Activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7013882/

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