我正在实现一个带有集合的小部件。我的小部件工作正常,但是,当用户单击集合中的一个项目时,我想打开我的应用程序一个 pass 和 id,从小部件到Main
activity
,然后将用户重定向到另一个activity
. 我这样做的原因是我需要先刷新Main
活动(我显示未读项目的数量)。
第一次单击小部件集合中的项目时,我所拥有的功能。但是,如果我按下主页按钮并单击另一个项目,我的应用程序只会显示Browser
活动的最后一个屏幕(用户被定向到的第二个活动)。如果我退出应用程序或单击返回,然后转到小部件,它会按预期工作。此问题仅在我“最小化”应用程序时发生,同时保留在 2nd 上activity
。
我不确定它是否与PendingIntent
小部件服务中的有关。
小部件服务
public class WidgetService extends RemoteViewsService {
@Override
public RemoteViewsFactory onGetViewFactory(Intent intent) {
return new StackRemoteViewsFactory(this.getApplicationContext(), intent);
}
}
class StackRemoteViewsFactory implements RemoteViewsService.RemoteViewsFactory {
private List<Integer> mIds = new ArrayList<Integer>();
private Context mContext;
private int mAppWidgetId;
public StackRemoteViewsFactory(Context context, Intent intent) {
mContext = context;
mAppWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
}
public void onCreate() {
}
public int getCount() {
// TODO Auto-generated method stub
return mIds.size();
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public RemoteViews getLoadingView() {
// TODO Auto-generated method stub
return null;
}
public RemoteViews getViewAt(int position) {
RemoteViews rv = new RemoteViews(mContext.getPackageName(), R.layout.widget_item);
final Intent mi = new Intent(mContext, Main.class);
final Bundle bun = new Bundle();
bun.putInt("id", mIds.get(position));
mi.putExtras(bun);
final PendingIntent piMain = PendingIntent.getActivity(mContext, position, mi, PendingIntent.FLAG_UPDATE_CURRENT);
rv.setOnClickPendingIntent(R.id.widget_text, piMain);
rv.setTextViewText(R.id.widget_text, mIds.get(position));
return rv;
}
public int getViewTypeCount() {
// TODO Auto-generated method stub
return 1;
}
public boolean hasStableIds() {
// TODO Auto-generated method stub
return true;
}
public void onDataSetChanged() {
mIds = Utilities.GetIds(); //returns id's from a sqlite database
}
public void onDestroy() {
// TODO Auto-generated method stub
}
}
主类
public class Main extends SherlockFragmentActivity
{
@Override
public void onCreate(final Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.main);
}
public void onNewIntent(Intent intent){
final Bundle extras = intent.getExtras();
final Intent browser = new Intent(this, Browser.class);
final Bundle bun = new Bundle();
bun.putInt("id", extras.getInt("id"));
browser.putExtras(bun);
startActivity(browser);
}
}
浏览器类
public class BrowserPager extends SherlockFragmentActivity
{
private int mId = 0;
@Override
public void onCreate(final Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.browser_pager);
final Bundle extras = getIntent().getExtras();
if (extras != null)
{
mId = extras.getInt("id");
}
...
}
}