This repository has been archived by the owner on Jun 23, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
3.13 Add ShareActionProvider to detail fragment menu
- Loading branch information
Lyla
committed
Mar 4, 2015
1 parent
745a01e
commit f63ba9f
Showing
3 changed files
with
52 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
f63ba9f
There was a problem hiding this comment.
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.
f63ba9f
There was a problem hiding this comment.
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.
f63ba9f
There was a problem hiding this comment.
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?
f63ba9f
There was a problem hiding this comment.
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.
f63ba9f
There was a problem hiding this comment.
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 ofDetailFragment
here whereas it is called inonCreate()
forForecastFragment
?f63ba9f
There was a problem hiding this comment.
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 theonCreate()
method and there was no difference. maybe both of them are called when the 'Detail Activity' is started.f63ba9f
There was a problem hiding this comment.
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?
f63ba9f
There was a problem hiding this comment.
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.
f63ba9f
There was a problem hiding this comment.
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 theActionProvider
that 's compatible withandroid.support.v7.widget.ShareActionProvider
. I think that there should be a hint for this under the lesson video in Udacity.f63ba9f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
f63ba9f
There was a problem hiding this comment.
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 addimport android.support.v4.view.MenuItemCompat;
.f63ba9f
There was a problem hiding this comment.
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.
private static String mForecastStr;
within DetailActivity and be set in the DetailFragment, as it would be accessible within it too.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?
f63ba9f
There was a problem hiding this comment.
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:
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);
f63ba9f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.