意向标志 - 如何启动一个新的活动实例,并有其他人关闭 [英] Intent flags - How to start a new activity instance and have others closed

查看:229
本文介绍了意向标志 - 如何启动一个新的活动实例,并有其他人关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我并不真正熟悉意图的标志,所以,请多多包涵。

我做这从一个ViewPager显示数据库中的东西的应用程序。的我也有不时翻出随机行并显示一个小部件。现在,当我点击它,我被带到其中显示相同内容的活动,现在我可以采取行动。

然而

一切正常,当我有一个活动已运行,然后点击窗口小部件,似乎它会创建一个新的实例,因为当我preSS后退按钮我不退出应用程序,但我带到了previous实例或什么的。

有没有开始活动的新实例的方式,如果有正在运行接近他们一些previous情况下,使用标志?

我CLEAR_TOP和东西发挥各地,但我真的不知道我在做什么。

谢谢!

//修改

Intent.FLAG_ACTIVITY_CLEAR_TOP似乎并没有做任何事情。 Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP似乎带来previous一到顶部,并忽略所述新的一个或东西。也许让我改一下,从我做起,从启动器图标的活动,那么我preSS的主页按钮,然后点击窗口小部件,它应该带来一个新的实例,其中ViewPager.setCurrentItem设置正确的页面(相同部件)。如果有以前没有运行的主要活动,一切都还好,但如果正在运行,完成它。

  MyWidgetProvider延伸AppWidgetProvider@覆盖
公共无效的onUpdate(上下文的背景下,AppWidgetManager appWidgetManager,INT [] appWidgetIds){
    Log.d(MainActivity,MyWidgetProvider#的onUpdate);    的for(int i = 0; I< appWidgetIds.length;我++){
        INT appWidgetId = appWidgetIds [I]        意向意图=新意图(背景下,MainActivity.class);
        intent.putExtra(from_widget,真正的);
        //intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        的PendingIntent的PendingIntent = PendingIntent.getActivity(背景下,0,意向,PendingIntent.FLAG_UPDATE_CURRENT);        RemoteViews意见=新的RemoteViews(context.getPackageName(),R.layout.widget_layout);
        views.setOnClickPendingIntent(R.id.widget_container,的PendingIntent);        appWidgetManager.updateAppWidget(appWidgetId,意见);
    }}


解决方案

有几种方法可以做到这一点。如果你想创建一个新的实例并关闭当前实例(假设它是在活动堆栈的顶部),你需要设置 Intent.FLAG_ACTIVITY_CLEAR_TOP

另外,也可以重新使用活动的同一实例,如果它已经在堆栈的顶部。在这种情况下,你需要将两个 Intent.FLAG_ACTIVITY_CLEAR_TOP Intent.FLAG_ACTIVITY_SINGLE_TOP 。如果你这样做,Android将无法创建活动的另一个实例,但会调用 onNewIntent()您的活动提供新的意图。的

I am not really familiar with intent flags, so please, bear with me.

I am doing an application which displays stuff from a database in a ViewPager. I also have a widget which from time to time pulls out a random row and displays it. Now, when I click on it, I am taken to an activity where the same content is displayed and now I can take action on it.

Everything works, however when I have an activity already running and then click on the widget, it seems it creates a new instance, because when I press the Back button I am not exiting the application but am taken to the previous instance or something.

Is there a way to start a new instance of an activity and if there were some previous instances running close them, using flags?

I played around with CLEAR_TOP and that stuff but I honestly don't know what am I doing.

Thanks!

//EDIT

Intent.FLAG_ACTIVITY_CLEAR_TOP doesn't seem to do anything. Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP seems to bring the previous one to the top and ignores the new one or something. Maybe let me rephrase, I start the activity from launcher icon, then I press the Home button and click on widget and it should bring up a new instance where ViewPager.setCurrentItem sets the correct page (same as in widget). If there were no main activity running before, everything is okay, but if it was running, finish it.

MyWidgetProvider extends AppWidgetProvider

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    Log.d("MainActivity", "MyWidgetProvider # onUpdate");

    for (int i = 0; i < appWidgetIds.length; i++) {
        int appWidgetId = appWidgetIds[i];

        Intent intent = new Intent(context, MainActivity.class);
        intent.putExtra("from_widget", true);
        //intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
        views.setOnClickPendingIntent(R.id.widget_container, pendingIntent);

        appWidgetManager.updateAppWidget(appWidgetId, views);
    }

}

解决方案

There are several ways to do this. If you want to create a new instance and close the current instance (assuming that it is on the top of the activity stack) you need to set Intent.FLAG_ACTIVITY_CLEAR_TOP.

Another alternative would be to reuse the same instance of the activity if it is already on the top of the stack. In this case you need to set both Intent.FLAG_ACTIVITY_CLEAR_TOP and Intent.FLAG_ACTIVITY_SINGLE_TOP. If you do this, Android won't create another instance of the activity, but will call onNewIntent() of your activity to deliver the new Intent.

这篇关于意向标志 - 如何启动一个新的活动实例,并有其他人关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆