Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass data and open an Activity from Widget? [Android]

In my ListProvider class that implements RemoteViewsFactory I have putted the following code below:

@Override
public RemoteViews getViewAt(int position) {
    RemoteViews rv = new RemoteViews(mContext.getPackageName(), R.layout.list_row);

rv.setTextViewText(R.id.heading, merch_name);
        rv.setTextViewText(R.id.content, teaser);

        Bundle extras = new Bundle();
        extras.putInt(WidgetProvider.EXTRA_ITEM, position);
        extras.putString(WidgetProvider.MERCHANT_ITEM, mWidgetItems.get(position).merchant_id);


        Intent fillInIntent = new Intent();
        fillInIntent.putExtras(extras);
        rv.setOnClickFillInIntent(R.id.llRow, fillInIntent);

  return rv;
}

I put Logs in my onReceive of WidgetProvider it has the correct ID when I clicked, but after opening the activity it doesn't have the correct ID where it is put in extras. There are time as well that it does not open the Activity I provided and just open my MainActivity. This happens when I removed my app from recently open app and then use the widget.

Here is my code for onReceive in my WidgetProvider

 public class WidgetProvider extends AppWidgetProvider {

 public static final String TOAST_ACTION = "my.packagename.TOAST_ACTION";
public static final String MERCHANT_ITEM = "my.packagename.MERCHANT_ITEM";

@Override
public void onReceive(Context context, Intent intent) {
    AppWidgetManager mgr = AppWidgetManager.getInstance(context);

    if (intent.getAction().equals(TOAST_ACTION)) {
        int appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
                AppWidgetManager.INVALID_APPWIDGET_ID);

        Intent goToDetails = new Intent(context, DetailsActivity.class);
        goToDetails.putExtra(Constants.MERCHANT_ID, intent.getStringExtra(MERCHANT_ITEM) );

        goToDetails.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(goToDetails);

      super.onReceive(context, intent);

    }

And this is how I get the ID in my DetailsActivity

 public class DetailsActivity extends Activity {

 String merchantID;

 @Override
protected void onCreate(Bundle savedState) {

 super.onCreate(savedState);
    setContentView(R.layout.detailsactivity);

  Bundle extras = getIntent().getExtras();
    if (extras != null) {
        merchantID = extras.getString(Constants.MERCHANT_ID);
    }

How to solve this? Thank you in advance.

like image 913
natsumiyu Avatar asked Sep 20 '16 06:09

natsumiyu


1 Answers

I added setData() in my onReceive where I pass data.

 @Override
public void onReceive(Context context, Intent intent) {
 super.onReceive(context, intent);

    if (intent.getAction().equals(TOAST_ACTION)) {
    int appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
            AppWidgetManager.INVALID_APPWIDGET_ID);

    Intent goToDetails = new Intent(context, DetailsActivity.class);
    goToDetails.putExtra(Constants.MERCHANT_ID, intent.getStringExtra(MERCHANT_ITEM) );

    goToDetails.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    goToDetails.setData(Uri.parse(goToDetails.toUri(Intent.URI_INTENT_SCHEME)));
    context.startActivity(goToDetails);



}

}

But I encounter a crash, if I un-install the app and then install and doesn't open the app first then add a widget and click on the ListView.

Found out why it crashes because of my database it still doesn't exist. Because I'm creating my database in my SplashActivity

like image 66
natsumiyu Avatar answered Sep 25 '22 11:09

natsumiyu