ViewPager + Fragments Starting New Actions

If I have a viewpager where each page is a fragment. And some of these snippets want to start other views / actions so that all these requests are passed to the parent activity. Thus, for each page in the view pager, all requests to start new actions and any results that need to be passed back to the fragments go through one main action. It seems like it will be messy. Is there a better way to handle this?

thank

+3


source to share


1 answer


This way, for each page in the view pager, all requests to start new actions and all the results to be passed back to the fragments go through one main action.

Since yours Fragments

lives and dies with their parent's lifecycle Activity

, yes, they should use their parent to trigger and get results from the other Activities

.

Since API 11 and in the latest support library, you can call these methods from yours Fragments

and they will execute from their parent Activity

for you.

    Intent intent = new Intent(getActivity(), SomeActivityToStart.class);
    startActivity(intent);

      

You can also run Activity

for the result internally Fragments

.



    Intent intent = new Intent(getActivity(), SomeActivityToStart.class);
    startActivityForResult(intent, REQUEST_CODE);

      

And then override onActivityResult(int requestCode, int resultCode, Intent data)

in Fragment

to get the result from the parent Activity

.

It seems like it will be messy. Is there a better way to handle this?

Since you don't need to manage the relationship between parent Activity

and child Fragment

yourself, this shouldn't be too messy in my opinion.

Checkout http://developer.android.com/reference/android/app/Fragment.html for a link to these methods.

+3


source







All Articles