我有一个具有配置活动的Android小部件.我还在窗口小部件上设置了ImageVIEw“按钮”以启动configure活动,以防用户在初始化后更改其首选项.
因此,基本生命周期:
>用户添加小部件
>d出配置活动,用户填写字段并单击“提交”
>窗口小部件已添加
>用户点击ImageVIEw以启动配置活动
>d出配置活动,用户编辑字段
>点击“提交”或退出后,小部件将更新
>用户可以根据需要继续执行步骤5和6.
我的问题在第5步.用户第一次(也是第一次)点击ImageVIEw,看起来好像启动了两个configure活动.也就是说,当我退出第一个时,后面还有另一个.但是,在随后的configure活动的所有启动中,只有一个启动,并且一切正常.
可能是什么问题呢?我将在下面发布相关代码.
AndroIDManifest.xml
@H_301_21@ <activity androID:name=".ConfigActivity" androID:label="@string/app_name" > <intent-filter> <action androID:name="androID.appWidget.action.APPWidget_CONfigURE" /> </intent-filter> </activity> <receiver androID:name=".Widget" androID:label="Widget" > <intent-filter> <action androID:name="androID.appWidget.action.APPWidget_UPDATE" /> </intent-filter> <intent-filter> <action androID:name="androID.appWidget.action.APPWidget_UPDATE" /> <data androID:scheme="sample_Widget" /> </intent-filter> <intent-filter> <action androID:name="com.this.that.Widget_CONTRol" /> <data androID:scheme="sample_Widget" /> </intent-filter> <Meta-data androID:name="androID.appWidget.provIDer" androID:resource="@xml/Widget" /> </receiver>
AppWidget-ProvIDer Widget.xml
@H_301_21@<?xml version="1.0" enCoding="utf-8"?><appWidget-provIDer xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:updatePeriodMillis="5000" androID:minWIDth="294dp" androID:minHeight="220dp" androID:initialLayout="@layout/Widgetlayout" androID:configure="com.this.that.ConfigActivity" ></appWidget-provIDer>
ConfigActivity.java
@H_301_21@@OverrIDepublic voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Get the data we were launched with Intent launchIntent = getIntent(); Bundle extras = launchIntent.getExtras(); if (extras != null) { appWidgetID = extras.getInt(AppWidgetManager.EXTRA_APPWidget_ID, AppWidgetManager.INVALID_APPWidget_ID); Intent cancelResultValue = new Intent(); cancelResultValue.putExtra(AppWidgetManager.EXTRA_APPWidget_ID, appWidgetID); setResult(RESulT_CANCELED, cancelResultValue); } else { // Only launch if it's for configuration finish(); } setContentVIEw(R.layout.myconfig); // Create buttons/EditTexts submitBTN = (button) findVIEwByID(R.ID.BTNsubmit); SampleET= (EditText) findVIEwByID(R.ID.ETSample); submitBTN.setonClickListener(submitListener); loadPreferences(ConfigActivity.this, appWidgetID);}private OnClickListener submitListener = new OnClickListener() { public voID onClick(VIEw v) { final Context context = PriorityVIEwConfig.this; // Save strings in our prefs String sample = SampleET.getText().toString(); SharedPreferences.Editor prefs = context.getSharedPreferences(PREFS_name, 0).edit(); prefs.putString(PREF_PREFIX_KEY + appWidgetID + "sample", sample); prefs.commit(); if (appWidgetID != AppWidgetManager.INVALID_APPWidget_ID) { // Tell the AppWidgetManager that we're Now configured Intent resultValue = new Intent(); //resultValue.setAction(AppWidgetManager.ACTION_APPWidget_UPDATE); resultValue.putExtra(AppWidgetManager.EXTRA_APPWidget_ID, appWidgetID); setResult(RESulT_OK, resultValue); // Get an instance of the AppWidgetManager AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context); // Update the App Widget with the layout RemoteVIEws vIEws = new RemoteVIEws(context.getPackagename(), R.layout.Widgetlayout); Widget.updatedisplayState(context, appWidgetID); } // Activity is Now done finish(); }};private voID loadPreferences(Context context, int appWidgetID) { SharedPreferences prefs = context.getSharedPreferences(PREFS_name, 0); String sample = prefs.getString(PREF_PREFIX_KEY + appWidgetID + "sample", null); if (sample != null) { SampleET.setText(sample); } else { // nothing stored, don't need to do anything }}
Widget.java
@H_301_21@@OverrIDepublic voID onReceive(Context context, Intent intent) { final String action = intent.getAction(); Log.d(LOG_TAG, "OnReceive:Action: " + action); if (ACTION_Widget_CONTRol.equals(action)) { // Pass this on to the action handler where we'll figure out what to do and update the Widget final int appWidgetID = intent.getIntExtra(AppWidgetManager.EXTRA_APPWidget_ID, AppWidgetManager.INVALID_APPWidget_ID); if (appWidgetID != AppWidgetManager.INVALID_APPWidget_ID) { this.onHandleAction(context, appWidgetID, intent.getData()); } } super.onReceive(context, intent);}public static voID updatedisplayState(Context context, int appWidgetID) { Intent configIntent = new Intent(context, ConfigActivity.class); configIntent.putExtra(AppWidgetManager.EXTRA_APPWidget_ID, appWidgetID); // Make this unique for this appWidgetID configIntent.setData(Uri.withAppendedpath(Uri.parse(Widget.URI_SCHEME + "://Widget/ID/"), String.valueOf(appWidgetID))); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, configIntent, PendingIntent.FLAG_UPDATE_CURRENT); vIEws.setonClickPendingIntent(R.ID.IVConfig, pendingIntent); AppWidgetManager.getInstance(context).updateAppWidget(appWidgetID, vIEws);}private voID onHandleAction(Context context, int appWidgetID, Uri data) { String controlType = data.getFragment(); // nothing here yet updatedisplayState(context, appWidgetID);}
我认为这些是最相关的部分.我要进一步研究的地方是ConfigActivity.java中的submitListener和Widget.java中的updatedisplayState方法.
任何帮助都是极好的!谢谢!
解决方法:
调用显式意图时,应考虑使用诸如FLAG_ACTIVITY_CLEAR_top之类的意图标志之一来使堆栈井井有条.例如:
@H_301_21@configIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_top);
您可以在某些应用程序(例如IMDB应用程序)中发现缺少此标志的情况,其中每次单击“主页”按钮都会在堆栈上添加另一个home活动实例,因此您需要多次单击“后退”按钮以在堆栈中d出并退出应用程序. 总结
以上是内存溢出为你收集整理的Android:两次启动Widget Config活动?全部内容,希望文章能够帮你解决Android:两次启动Widget Config活动?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)