-1

I want to add multiple activities under one `onCreateOptionsMenu(Menu menu) in my android app, I have already added two activities and they are working fine but third activity isn't working, following is my code

onCreateOptionsMenu(Menu menu)

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public final boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.menu_item_share:
            shareURL();
    }
    if(item.getItemId() == R.id.menu_item_refresh){
        mWebView.reload();
        return true;
    }
    if(item.getItemId() == R.id.share_this_app)
        mShareActionProvider.setShareIntent(getDefaultShareIntent());
    return super.onOptionsItemSelected(item);
}


private void shareURL() {
    Intent shareIntent = new Intent(Intent.ACTION_SEND);
    shareIntent.setType("text/plain");
    shareIntent.putExtra(Intent.EXTRA_TEXT, mWebView.getUrl());
    startActivity(Intent.createChooser(shareIntent, "Share This Website!"));
    shareIntent.setPackage("com.whatsapp");
}


/** Returns a share intent */
private Intent getDefaultShareIntent(){
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("text/plain");
    intent.putExtra(Intent.EXTRA_SUBJECT, "download the app");
    intent.putExtra(Intent.EXTRA_TEXT," play.google.com ");
    return intent;
}

menu_main.xml

<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">

<item
    android:title="@string/share"
    android:id="@+id/menu_item_share"
    android:showAsAction="always"
    android:icon="@drawable/share"
    />

<item
    android:id="@id/menu_item_refresh"
    android:title="Refresh"
    android:showAsAction="never"
    android:icon="@drawable/refresh"
    />

<item
    android:id="@+id/share_this_app"
    android:title="Share this app"
    android:showAsAction="never"
    android:actionProviderClass="android.widget.ShareActionProvider"/>

From above, menu_item_share and menu_item_refresh is working, but Share this app isn't working.

sagar chaudhary
  • 33
  • 1
  • 12
  • There is no `return true;` for the last one. – greenapps Jul 04 '16 at 17:55
  • Put all possibilities in that switch statement. Or use only ifs. – greenapps Jul 04 '16 at 17:55
  • `I have already added two activities` ??? Activities? Nowhere i see an Activity. – greenapps Jul 04 '16 at 18:06
  • `but Share this app isn't working.` Share this app ? Can't you be precise? And what is `not working`? What should happen that does not happen? You are only setting an intent. Nowhere you start an activity there. – greenapps Jul 04 '16 at 18:15
  • @greenapps now i have added menu_main.xml to question as well, by share this app isn't working. i mean after clicking on share this app option it should be able to send my apps google play store link to other apps, but the share this app option isn't responding . – sagar chaudhary Jul 04 '16 at 18:27
  • `it should be able to send my apps google play store link to other apps, ` Really? Why do you thinkk it would? You are only setting an intent. You are not starting it. – greenapps Jul 04 '16 at 18:29
  • `mShareActionProvider`. You are not showing what that is and how it is instantiated. – greenapps Jul 04 '16 at 18:30
  • http://stackoverflow.com/questions/17928709/using-android-action-bar-share-intent – greenapps Jul 04 '16 at 18:41
  • `How to add multiple activities`. Pretty bad description of your problem. – greenapps Jul 04 '16 at 18:44
  • @greenapps it worked, thank you very much. – sagar chaudhary Jul 04 '16 at 18:54
  • @greenapps i quoted question like that because i wasn't aware about how to use single onCreate for multiple actions. – sagar chaudhary Jul 04 '16 at 18:57

2 Answers2

0

It's probably your are missing a return true statement below this line mShareActionProvider.setShareIntent(getDefaultShareIntent());. You can simply follow a clean structure to achieve your task.

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.menu_item_share: {
        shareURL();
        break; //or, return true;
    }
    case R.id.menu_item_refresh: {
        mWebView.reload();
        break; //or, return true;
    }
    case R.id.share_this_app: {
        mShareActionProvider.setShareIntent(getDefaultShareIntent());
        break; //or, return true;
    }
    return super.onOptionsItemSelected(item);
}
Sudip Podder
  • 830
  • 11
  • 25
  • not working, now its force closing the app after clicking on share this app, i have added both share intents in question, if you can have a look at it and let me know if there is something wrong with them. – sagar chaudhary Jul 04 '16 at 18:19
  • you already have used what you need in shareURL() method. (1) create a method like 'shareURL()'. (2) follow: https://developer.android.com/training/sharing/send.html to share your app_playstore_url via other apps. (3) call the new method when you press on R.id.share_this_app – Sudip Podder Jul 04 '16 at 18:48
  • you can accept/upvote the answer if it was helpful. Your little inspiration encourages us to spend more time here :) – Sudip Podder Jul 04 '16 at 19:02
0

Hey I think this would help... You have written android:showAsAction="never".
So it would generate an error while putting an icon inside the menu icon(three dots at toolbar). Hence put the value "always" or "ifRoom" for this attribute. Hope this would work :-)

Anurag Sidana
  • 61
  • 1
  • 6