简体   繁体   English

意向和onNewIntent

[英]intent and onNewIntent

I've an app that has a main menu screen called NfcScannerActivity. 我有一个应用程序,具有一个名为NfcScannerActivity的主菜单屏幕。 At the moment it has no launch mode (standard) in the manifest. 目前,清单中没有启动模式(标准)。 If you click on getRota it takes you to the rota screen which is defined in the manifest as 'singleTask' and is simply a listview of data from a webcall. 如果单击getRota,将带您到清单屏幕中定义为“ singleTask”的旋转屏幕,它只是Web调用中数据的列表视图。

In the rota screen you can click on nextRota from the optionsMenu bar. 在旋转屏幕中,您可以从选项菜单栏中单击nextRota。 When this happens, an intent is launched specifying the menu screen(NfcScannerActivity) as this is where the webcall is made to get the next day's rota data. 发生这种情况时,将启动一个指定菜单屏幕(NfcScannerActivity)的意图,因为这是进行网络呼叫以获取第二天的轮转数据的位置。 once the data is retrieved the rota screen is launched again. 检索到数据后,将再次启动旋转屏幕。

All this works fine but i'm convinced that there are some problems in the app due to having more than one instance of the menu screen in the task. 所有这些工作正常,但我确信由于任务中有多个菜单屏幕实例,因此应用程序中存在一些问题。 If i specify NfcScannerActivity to be 'SingleTask' then when you click next rota it stays on the menu screen as if it hasn't handled the "NEXT_ROTA" intent action. 如果我将NfcScannerActivity指定为'SingleTask',则当您单击下一个Rota时,它将停留在菜单屏幕上,就好像它没有处理“ NEXT_ROTA”意向操作一样。

I understand that i might have to override onNewIntent in the NfcScannerActivity activity. 我了解我可能必须重写NfcScannerActivity活动中的onNewIntent。

How is this done? 怎么做? I've tried the following. 我已经尝试了以下方法。

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

.

This doesn't seem to handle the 'NEXT_ROTA' intent action. 这似乎无法处理“ NEXT_ROTA”意图动作。 thanks matt. 谢谢马特。

[edit1] [edit1]

this is what i have in the rota activity when the user clicks next_rota from the options menu. 这是当用户从选项菜单中单击next_rota时我在rota活动中所拥有的。

Intent i = new Intent(this, NfcscannerActivity.class);
            i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            i.putExtra("nextRota", nextDay);
            i.setAction("NEXT_ROTA");
            startActivity(i);

.

Then in the onCreate of NfcScannerActivity i have the following. 然后在NfcScannerActivity的onCreate中,我有以下内容。

if(intent.getAction().equalsIgnoreCase("NEXT_ROTA")){

            Log.e(TAG, "next rota action");
            String date = intent.getStringExtra("nextRota");



            getNextRota(date);


        }

.

getNextRota(date) calls an AsyncTask to make the webcall to get the data of the next rota. getNextRota(date)调用AsyncTask进行网络调用以获取下一个旋转数据。 In the onPostExecute it the does the following. 在onPostExecute中,它执行以下操作。

Intent intent = new Intent(NfcscannerActivity.this,
                            GetRota.class);
             Bundle b = new Bundle();
             b.putSerializable("rotaArray", rotaArray);


             intent.putExtra("rotaArrayBundle", b);
             startActivity(intent);

.

So i'm already handling the 'NEXT_ROTA' intent action in NfcScannerActivity inside onCreate. 所以我已经在onCreate的NfcScannerActivity中处理了'NEXT_ROTA'意向动作。 Do i have to do the same in onNewIntent? 我必须在onNewIntent中做同样的事情吗?

Try the following 尝试以下

In the rota screen Activity when "an intent is launched specifying the menu screen" 在轮转屏幕活动中,当“启动意图以指定菜单屏幕”时

Intent intent = new Intent(<rota screen activity>, NfcScannerActivity.class);
intent.setAction("NEXT_ROTA");
//this brings the previous existing activity to the front of the stack 
//instead of creating a new one
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);

In the NfcScannerActivity 在NfcScannerActivity中

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

    if(intent.getAction().equals( "NEXT_ROTA")){
        String date = intent.getStringExtra("nextRota");
        getNextRota(date);      
    }       
}

What the above should do is allow you create as many Rota Screens as you want but only ever have the one NfcScannerActivity 上面应该做的就是允许您创建任意数量的Rota屏幕,但只能拥有一个NfcScannerActivity

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM