如何使用putExtra()与FLAG_ACTIVITY_REORDER_TO_FRONT在Android应用?如何使用、putExtra、Android、FLAG_ACTIVITY_REORDER_

2023-09-06 01:43:42 作者:何以清尘

我有一个应用程序,称之为App1的。在我App1的我调用相机应用程序。

I have an application, call "App1". In my App1 I invoke Camera application.

intent = new Intent(Intent.ACTION_MAIN);
intent.setComponent(new ComponentName("com.android.camera","com.android.camera.Camera"));
startActivity(intent);

在这之后,我用FileObserver倾听用户是否拍照。当发生这种情况我称之为

After that, I use FileObserver to listen whether user take a photo. When this happens I call

Context ctx = App1.this.getApplicationContext();
Intent j = new Intent(ctx, App1.class);
j.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
j.putExtra("type", 1);
startActivity(j);

它的工作原理,我的意思是得到我的应用程序前我怎么离开它,但是我需要传递一个整数,这就是所谓的类型。我想,我的应用程序将调用onResume(),但我怎么能获得额外的整数。这种类型的传递并没有在我的应用程序做任何事情。

It works, I mean it gets my application to front how I leave it, however I need to pass an integer, which is called "type". I think, my application will call "onResume()" but how can I get that extra integer. This type of passing didn't do anything in my application.

有捆绑savedInstanceState在onCreate方法,但在onResume方法没有这样的事情。所以,我需要你的帮助来解决这个问题。先谢谢了。

There's Bundle savedInstanceState in onCreate method, but there is no such a thing in onResume method. So, I need your help to solve this problem. Thanks in advance.

推荐答案

您需要重写Activity.onNewIntent()方法。

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    setIntent(intent);
}

onNewIntent()被调用,在正常 onResume()的生命周期将随之而来。或者,你可以写在 onNewIntent您code()本身。

After onNewIntent() is called, the normal onResume() lifecycle will follow. Alternatively, you can write your code in onNewIntent() itself.