nss
nss

Reputation: 121

How to start new activity in each item list on App Widget

I have an App Widget that contains a ListView layout. When I click on item list the activity is open, but if I press Home Button and click on other item list the activity in background is open again. I want that each item list opens a new Activity and close other Activity if already exists.

WidgetProvider.class

        RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
                R.layout.widgetnews);

        remoteViews.setImageViewBitmap(R.id.widgetTitle, buildUpdate("TITLE"));

        // Intent to item List
        Intent intentActivity;
        if(context.getResources().getBoolean(R.bool.tablet_full))
            intentActivity = new Intent(context, MainTabletFull.class); 
        else
            intentActivity = new Intent(context, MainSmartphone.class);
        intentActivity.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
        intentActivity.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        intentActivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intentActivity.setData(Uri.parse(intentActivity.toUri(Intent.URI_INTENT_SCHEME)));
        intentActivity.addCategory(Intent.CATEGORY_HOME);

        PendingIntent pendingIntent = PendingIntent.getActivity(context, 1, intentActivity, 0);
        remoteViews.setOnClickPendingIntent(R.id.widgetlogo, pendingIntent);
        remoteViews.setPendingIntentTemplate(R.id.widgetlist, pendingIntent);


        // Intent to Widget Logo
        Intent intent = new Intent(context, WidgetService.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addCategory(Intent.CATEGORY_HOME);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
        intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
        remoteViews.setRemoteAdapter(R.id.widgetlist, intent);

        appWidgetManager.updateAppWidget(widgetId, remoteViews);

WidgetRemoteViewsFactory

@Override
public RemoteViews getViewAt(int position)
{
    RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.widget_noticias_row);

    remoteView.setTextViewText(R.id.wnr_date, "DATE");
    remoteView.setTextViewText(R.id.wnr_title, "TITLE");

    if(artigo.getId()!=null)
    {
        Intent intentActivity = new Intent();
        intentActivity.putExtra("ID_NOTIF", "ID");
        intentActivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        remoteView.setOnClickFillInIntent(R.id.wnr_layout, intentActivity);
    }

    return remoteView;
}

Upvotes: 0

Views: 579

Answers (2)

Code-Apprentice
Code-Apprentice

Reputation: 83557

I think you are making this more complicated than you need to. Simply remove all of the following:

    intentActivity.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
    intentActivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intentActivity.setData(Uri.parse(intentActivity.toUri(Intent.URI_INTENT_SCHEME)));
    intentActivity.addCategory(Intent.CATEGORY_HOME);

Upvotes: 1

CmosBattery
CmosBattery

Reputation: 954

Perhaps your looking for...

intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); // or intent.setFlags

or

android:noHistory="true"

"Whether or not the activity should be removed from the activity stack and finished (its finish() method called) when the user navigates away from it and it's no longer visible on screen" -> http://developer.android.com/guide/topics/manifest/activity-element.html#nohist

Or possibly work in a way to directly call finish() when the activity is left.

Also see: Clear the entire history stack and start a new activity on Android

Android: Remove all the previous activities from the back stack

http://www.slideshare.net/RanNachmany/manipulating-android-tasks-and-back-stack

etc.

I also can't see everything your doing but your starting new tasks in some way and so FLAG_ACTIVITY_CLEAR_TASK & FLAG_ACTIVITY_NEW_TASK may or may not be pointless. There can be multiple tasks on the back stack that will not be cleared. As such you may need to look at how all your activities are being launched from the manifest as well as all the intents in code.

If your talking about reopening the same activity and refresh it that can be done as well using the above stuff and

intent = getIntent();
finish();
startActivity(intent);

Android refresh current activity

Upvotes: 1

Related Questions