anothertest
anothertest

Reputation: 979

Impossible to pass string through activities at the beginning

I am trying to pass a string from an activity to another activity. I need this string to send an Http request with a specific key (the string) as a header.

Intent intent = new Intent(getActivity(), StatusActivity.class).putExtra(Intent.EXTRA_TEXT,"");
intent.putExtra("token", token);
startActivity(intent);

I try to get the value in the other activity:

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_status, container, false);
        ListView listView = (ListView) rootView.findViewById(R.id.mylist);
        //

        Intent intent = getActivity().getIntent();
        token = intent.getExtras().getString("token");
        adapter = new ObjAdapter(getActivity() ,new ArrayList<Post>());
        listView.setAdapter(adapter);
        return rootView;
    }

But the problem is that I initialize an ArrayAdapter inside the method onCreate, because I want the data to be present at the beginning of the activity. It seems that the token is null and cannot be found.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_status);
    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new StatusFragment())
                .commit();
    }
    FeedAsync task = new FeedAsync();
    task.execute();
}

How can I get the string token at the beginning of the activity? I need the string token to execute the AsyncTask.

Upvotes: 0

Views: 47

Answers (1)

eleven
eleven

Reputation: 6847

The "most beginning" place in Activity is onCreate method after calling of super method. There is no earliest place where you could get Intent data.

Upvotes: 1

Related Questions