Melisa
Melisa

Reputation: 41

Can't implement getIntent(); method in Fragment

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

    }


}

Upvotes: 0

Views: 8503

Answers (2)

Sergio
Sergio

Reputation: 30705

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.

Upvotes: 5

kira_codes
kira_codes

Reputation: 1616

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.

Upvotes: 0

Related Questions