Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intent and putExtra does not work

I'm going crazy with Android programming. Nothing works properly..

Whats wrong with this?

Error: getIntent() is undefined for type View

Any ideas?

public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_quiz, container,
                false);

        TextView text = (TextView)rootView.findViewById(R.id.ttv);

        Bundle intentBundle = rootView.getIntent().getExtras();
        int question_cat = intentBundle.getInt("question_cat");

        text.setText(question_cat);

        return rootView;
    }
}
like image 519
Philip Avatar asked Nov 30 '25 02:11

Philip


1 Answers

You can just do:

Intent intentBundle = getActivity().getIntent();
        String question_cat = intentBundle.getStringExtra("question_cat");
        Log.i("Result : ", question_cat);

Even after you get the value as string, you use it as a int value later like this :

int j = Integer.valueOf(question_cat);

    Log.i("Result : ", String.valueOf(j));

For your other question, with getIntent(), the problem is that you are using it inside the fragment class and in order to use that, you have to use getActivity() to access it. If it was just a normal activity, it wasn't that complicated. Android is really fun if some concepts are clear .. :)

like image 135
mike20132013 Avatar answered Dec 02 '25 17:12

mike20132013