简体   繁体   中英

Start Intent activity from fragment fail - android

I want to start the activity from fragment but fail.

Here is my code

MainActivity myactivity = (MainActivity) getActivity();


Intent intent = new Intent(myactivity, PopUpImageActivity.class);
Bundle extras = new Bundle();
myactivity.startMyIntent(intent);

尝试这个:

    startActivity(new Intent(getActivity().getApplicationContext(), YourActivity.class));

Instead of doing the intent from your activity , why not doing the intent on the fragment class itself?

For example,

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

This should work and there's no need to access your activity because all fragment resides on an activity anyway and it already knows by itself which activity it is residing without having it specified

The answer above may/maynot be right but for the best practice always allow transactions through the Activity which enclose the respective Fragment .

Make use of interfaces , onAttach () in your Fragment for giving call to the enclosing Activity & from the activity, allow for another activity/fragment transaction.

A sample code would look like this:

public class MyFragmentExample extends Fragment{

//Creating instance of Interface
AnyInterfaceName anyInterfaceName;


/*
This onAttach is responsible for attaching the interface
listener of fragment to Activity
*/
 @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        try {
            anyInterfaceName = (AnyInterfaceName) context;
        } catch (ClassCastException ex) {
            throw new ClassCastException(ex.getMessage() + "must implement AnyInterfaceName");
        }
    }

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return view;
    }


// Define an interface with any name
public interface AnyInterfaceName{
    /*
      you can give any name to this function & this
      function will be implemented later in activity
    */
    void startAnotherActivity();
}
}

Now on the Activity side: add this on top just after extends Activity/AppCompActivity

implement MyFragment.AnyInterfaceName

After this, just implement the method & inside the method, allow transiting to Another Activity as you do it from Activity using Intent .

Note: Don't forget to call to the interface in fragment while you want for starting another Activity. Simply call it like this:

anyInterfaceName.startAnotherActivity();

It can be a lengthy work to do but for the best practice, it is.

Check out from official android site Communicating with the Activity here

Hope it helps !!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM