Skip to content
This repository has been archived by the owner on Jun 23, 2022. It is now read-only.

Commit

Permalink
3.13 Add ShareActionProvider to detail fragment menu
Browse files Browse the repository at this point in the history
  • Loading branch information
Lyla committed Mar 4, 2015
1 parent 745a01e commit f63ba9f
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
Expand Up @@ -20,8 +20,12 @@
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.widget.ShareActionProvider;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
Expand Down Expand Up @@ -69,7 +73,13 @@ public boolean onOptionsItemSelected(MenuItem item) {
*/
public static class DetailFragment extends Fragment {

private static final String LOG_TAG = DetailFragment.class.getSimpleName();

private static final String FORECAST_SHARE_HASHTAG = " #SunshineApp";
private String mForecastStr;

public DetailFragment() {
setHasOptionsMenu(true);
}

@Override
Expand All @@ -81,12 +91,42 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
// The detail Activity called via intent. Inspect the intent for forecast data.
Intent intent = getActivity().getIntent();
if (intent != null && intent.hasExtra(Intent.EXTRA_TEXT)) {
String forecastStr = intent.getStringExtra(Intent.EXTRA_TEXT);
mForecastStr = intent.getStringExtra(Intent.EXTRA_TEXT);
((TextView) rootView.findViewById(R.id.detail_text))
.setText(forecastStr);
.setText(mForecastStr);
}

return rootView;
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// Inflate the menu; this adds items to the action bar if it is present.
inflater.inflate(R.menu.detailfragment, menu);

// Retrieve the share menu item
MenuItem menuItem = menu.findItem(R.id.action_share);

// Get the provider and hold onto it to set/change the share intent.
ShareActionProvider mShareActionProvider =
(ShareActionProvider) MenuItemCompat.getActionProvider(menuItem);

// Attach an intent to this ShareActionProvider. You can update this at any time,
// like when the user selects a new piece of data they might like to share.
if (mShareActionProvider != null ) {
mShareActionProvider.setShareIntent(createShareForecastIntent());
} else {
Log.d(LOG_TAG, "Share Action Provider is null?");
}
}

private Intent createShareForecastIntent() {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT,
mForecastStr + FORECAST_SHARE_HASHTAG);
return shareIntent;
}
}
}
9 changes: 9 additions & 0 deletions app/src/main/res/menu/detailfragment.xml
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/action_share"
android:title="@string/action_share"
app:showAsAction="always"
app:actionProviderClass="android.support.v7.widget.ShareActionProvider" />
</menu>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Expand Up @@ -14,6 +14,7 @@
-->
<string name="action_settings">Settings</string>
<string name="action_map">Map Location</string>
<string name="action_share">Share</string>

<!-- Menu label to fetch updated weather info from the server -->
<string name="action_refresh" translatable="false">Refresh</string>
Expand Down

14 comments on commit f63ba9f

@t2hv33
Copy link

@t2hv33 t2hv33 commented on f63ba9f May 11, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is detail_text
I can't find it in another xml file.

@serxioai
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should check the .xml layout file under /res for the revision you have checked out. The detail_text should be—but you should verify—in the fragment_detail.xml. The detail_text will have the weather for a particular day that is sent via ShareIntent to another app such as email, text message, Google Hangouts, etc.

@vmarasow
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the first file the DetailActivity or the DetailFragment?

@evrencoskun
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First file is DetailActivity which has DetailFragment as an inner class. Last update to android studio, when user create a activity with a fragment, Then It creates them as two separate files. So If you have a DetailFragment file, you should use this file.

@triarius
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is setHasOptionsMenu(true) called in the constructor of DetailFragment here whereas it is called in onCreate() for ForecastFragment?

@gunbladerq
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@triarius I put setHasOptionsMenu(true) in the onCreate() method and there was no difference. maybe both of them are called when the 'Detail Activity' is started.

@peterqz
Copy link

@peterqz peterqz commented on f63ba9f Mar 2, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change onCreateOptionsMenu inside DetailFragment, Why this?

@kauron
Copy link

@kauron kauron commented on f63ba9f Mar 5, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@peterqz because the text shared can only be accessed from the Fragment, therefore the menu must be the one from the DetailFragment.

@toby-1-kenobi
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have to be in the know to use MenuItemCompat (DetailActivity.java:112) in order to get the menu item to give you a version of the ActionProvider that 's compatible with android.support.v7.widget.ShareActionProvider. I think that there should be a hint for this under the lesson video in Udacity.

@rahulsther
Copy link

@rahulsther rahulsther commented on f63ba9f Jul 18, 2016 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@roshan92
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rahulsther I had problem with gradle and MenuItemCompat as well. All I did was to clean project and add import android.support.v4.view.MenuItemCompat;.

@RafayAK
Copy link

@RafayAK RafayAK commented on f63ba9f Aug 28, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can anyone explain why the Share Intent was implemented in the Fragment and not the Activity.

  1. Creating the share menu item in detail.xml seems more straightforward as it already exists(i.e no need to create detailfragment.xml)
  2. The string mForecastStr can be set as global variable private static String mForecastStr; within DetailActivity and be set in the DetailFragment, as it would be accessible within it too.
  3. Since the onCreateOptionsMenu is already created in the Activity we just need to fill it with appropriate lines just get the share intent working.

What I am trying to say is I got the Share Intent working while making changes in the Activity and its XML, is there a benefit to creating the Share Intent within the Fragment, instead?

@ruairimcguigan
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rahulsther
I had the same problem and I have found the solution:

  1. You have to use:
<item
    android:id="@+id/menu_share"
    android:title="@string/menu_share"
    bwq:actionProviderClass="android.support.v7.widget.ShareActionProvider"
    bwq:showAsAction="always"/>
  1. and in Java

import android.support.v7.widget.ShareActionProvider;
and

// Locate MenuItem with ShareActionProvider
MenuItem item = menu.findItem(R.id.menu_share);
// Fetch and store ShareActionProvider
mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(item);

@toby-1-kenobi
Copy link

@toby-1-kenobi toby-1-kenobi commented on f63ba9f Sep 8, 2016 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.