【问题标题】:How open same activity with different extras from widget如何使用来自小部件的不同附加功能打开相同的活动
【发布时间】:2014-05-31 04:08:37
【问题描述】:

我使用小部件帮助用户显示重要数据并从小部件打开应用程序。

我的小部件有 3 个按钮,当用户点击不同的按钮时,我需要打开应用程序。 我需要打开相同的活动,但根据用户单击的按钮使用不同的数据。我有意传递不同的额外内容

例如

Ist 按钮我将用户名设置为“Ranjith”

对于第二个按钮,我将用户名设置为“Thomas”

对于第三个按钮“Rixon”

但当活动打开时,它总是显示 Rixon 第三个。

         logIntent= new Intent(context, MainActivity.class);
            earnIntent=new Intent(context, MainActivity.class);
            connectIntent=new Intent(context, MainActivity.class);
            logIntent.putExtra("user_name", "Ranjith");
            connectIntent.putExtra("user_name", "Thomas");
            earnIntent.putExtra("user_name", "Rixon");
    }
    remoteViews.setOnClickPendingIntent(R.id.bottom_of_widget_log,createPendingIntent(context, 0, logIntent));
        remoteViews.setOnClickPendingIntent(R.id.bottom_of_widget_earn, createPendingIntent(context, 1, earnIntent));
        remoteViews.setOnClickPendingIntent(R.id.bottom_of_widget_connect, createPendingIntent(context, 2, connectIntent));
    AppWidgetManager manager = AppWidgetManager.getInstance(context) ;
manager .updateAppWidget(thisWidget, remoteViews);

【问题讨论】:

  • 发布你的代码createPendingIntent()

标签: android android-activity android-widget android-pendingintent


【解决方案1】:

关于活动,您必须检查清单中的 android:launchMode 选项。

正如Documentation中所说,

具有“标准”或“singleTop”启动模式的活动可以多次实例化[....] 相反,“singleTask”和“singleInstance”活动只能开始一个任务。它们始终位于活动堆栈的根部。此外,设备一次只能保存一个 Activity 实例——只能完成一个这样的任务。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-16
    • 2012-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-22
    相关资源
    最近更新 更多