74569

Can't implement getIntent(); method in Fragment

Question:

I try to implement getIntent(); in Fragment and display text but I get Cannot resolve method 'getIntent()'

Here is part of Fragment3.java

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_3, container, false); TextView textViewDisplayResult = (TextView) getView().findViewById(R.id.text_view_display_result); textViewDisplayResult.setText(getIntent().getBooleanExtra("KEY_ANSWER", false)?R.string.Good_answer:R.string.Wrong_answer);

Here is xml TextView for displaying text

<TextView android:id="@+id/text_view_display_result" android:layout_width="match_parent" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge"/>

And part of MainActivity regarding this matter

final Intent intent = new Intent(MainActivity.this, Fragment3.class); buttonCheckAnswer.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (checkAnswers()) intent.putExtra("Key Answer", true); else intent.putExtra("Key Answer", false); startActivity(intent); } });

And here is the whole Fragment3

package make.appaplication; import android.content.Intent; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; /** * A simple {@link Fragment} subclass. */ public class Fragment41 extends Fragment { public Fragment41() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_3, container, false); TextView textViewDisplayResult = (TextView) getView().findViewById(R.id.text_view_display_result); textViewDisplayResult.setText(getActivity().getIntent().getB‌​ooleanExtra("KEY_ANS‌​WER", false)?R.string.Good_answer:R.string.Wrong_answer); } }

Answer1:

You can use:

getActivity().getIntent();

Or you can pass data to fragment directly using arguments:

Bundle args = new Bundle(); args.putBoolean("Key Answer", true); Fragment f = new YourFragment(); f.setArguments(args);

Make sure you set arguments before fragment is added to FragmentManager.

Answer2:

getIntent() is a Activity class method which isn't directly accessible from your fragment. Try calling getActivity.getIntent() to get the Activity in context first.

Recommend

  • Is it possible to save the state of a button in one activity and then pass it on to a new activity?
  • Testing custom SOAP Headers in ASMX
  • Failed to read schema document 'http://www.springframework.org/schema/tool/spring-tool-4.1.xsd&
  • Upload a video to Youtube using the python API and set it as unlisted
  • Keep same scale in different graphs ggplot2
  • Unexpected Error Google Apps Script Fetch
  • Change Divider Color Android DatePicker Dialog
  • Vue props data not updating in child component
  • Deploying pre-encrypted configuration files to a production environment
  • How can I include the Ivy dependency and none of its dependencies?
  • how to convert open xml representation of word document into RTF format using c#.net 4.0?
  • Dealing with XElement null value
  • Updates to SolrConfig.xml file are not being reflected
  • How to check disabled jobs with Jenkins server?
  • Android NFC read Tags issue. Activity starts each time on data received
  • Android - VerifyError
  • Phonegap GA plugin not working
  • CloseOptionsMenu doesn't work?
  • how to ignore xml namespaces?
  • Activity overlaying. WindowManager.LayoutParams
  • iOS Cordova first plugin - plugin.xml to inject a feature
  • Splash Screen will not display
  • Doctrine/Symfony entity generator and generating entity from one table
  • Xamarin Android | Layout style
  • Setting the run time properties on SpringApplicationBuilder()
  • Enabling DTD support in Sql Server
  • where do I find the xml.dom python package for the python-2.6.0-8.9.28 and I have a suse/x86_64 vers
  • How to get current document uri in XSLT?
  • Array with custom indexes in Ionic2
  • Textfile Structure (tables)
  • Xcode 4 NSLog Macro link in Xcode 3
  • Play WS (2.2.1): post/put large request
  • Deserializing XML into class C#
  • How to set my toolbar fixed while scrolling android
  • AT Commands to Send SMS not working in Windows 8.1
  • Is there a mandatory requirement to switch app.yaml?
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • apache spark aggregate function using min value
  • Sorting a 2D array using the second column C++
  • How to Embed XSL into XML