Sainath S.R
Sainath S.R

Reputation: 3286

Can't understand why string value turns to null suddenly?

Hers is my code

 public static class DetailFragment extends Fragment {
        private String forecastData;

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

        private static final String FORECAST_SHARE_HASHTAG ="#SunshineApp";

        public DetailFragment() {

        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setHasOptionsMenu(true);

        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
           View rootView = inflater.inflate(R.layout.fragment_detail, container, false);
            //receive forecast data from the ForeCast Fragment
            Intent intent=getActivity().getIntent();
            forecastData=intent.getStringExtra(Intent.EXTRA_TEXT);
            Log.v(LOG_TAG,"data is "+forecastData);
            TextView textView=(TextView)rootView.findViewById(R.id.detail_text);
            textView.setText(forecastData);

            return rootView;
        }

        @Override
        public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
            super.onCreateOptionsMenu(menu, inflater);
            inflater.inflate(R.menu.menu_detailfragment,menu);

            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(createShareIntent());

            }
            else
            {
                Log.v(LOG_TAG,"Share Action Provider is null");
            }
        }
        public  Intent createShareIntent()
        {
            Log.v(LOG_TAG,"data is "+forecastData);
            Intent intent=new Intent(Intent.ACTION_SEND);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
            intent.setType("text/plain");
            intent.putExtra(Intent.EXTRA_TEXT,forecastData+DetailFragment.FORECAST_SHARE_HASHTAG);
            Log.v(LOG_TAG,"data is "+forecastData);
            return  intent;
        }

I am absolutely sure the intent is fired correctly because i can share with other apps and it works ,It's clear if you see the logs below

01-04 18:36:02.370    6121-6121/com.example.droid.sunshine V/DetailFragment﹕ in optionMenu method data is null
01-04 18:36:02.380    6121-6121/com.example.droid.sunshine V/DetailFragment﹕ in create intent data is null
01-04 18:36:02.380    6121-6121/com.example.droid.sunshine V/DetailFragment﹕ in create intent data is null
01-04 18:36:02.650    6121-6121/com.example.droid.sunshine V/DetailFragment﹕ in view method data is Wed, Jan 7 - Clear - 27/24

Why is this , i thought OnCreate() view method was called before OnCreateOptionsMenu() ?what do i do to rectify this?

Upvotes: 0

Views: 266

Answers (2)

aschattney
aschattney

Reputation: 329

imho you should move your code from onCreateView() to onActvivityCreated().

onCreateView is meant to return the view.

onActivityCreated means that the associated activity has completed its method onCreate() and is set up.

Upvotes: 0

AndreiBogdan
AndreiBogdan

Reputation: 11164

Here's an idea:

  • According to your logs, the outputs are FIRST displayed from the onCreateOptionsMenu() method and only the last one is from the onCreateView(). That would mean that onCreateOptionsMenu() is called before onCreateView which would explain why everything is null.

  • Try moving setHasOptionsMenu(true) somewhere else, maybe in the constructor That should make sure that onCreateOptionsMenu() is called after onCreate and onCreateView. Try it.

Upvotes: 3

Related Questions