简体   繁体   中英

Declare activity in AndroidManifest.xml to use an Intent

I have the following Intent object I'm trying to convey from the fragment SeventhFragment.java to another fragment (SixthFragment.java).

Intent i = new Intent(getContext(), SixthFragment.class);
i.putExtra("officeHour", hour);
startActivity(i); // this code is in SeventhFragment.java

In SixthFragment.java, I have the following code to try to get this object back:

Intent intent = (Intent) getActivity().getIntent(); // this code is in SixthFragment.java.
OfficeHour add = (OfficeHour) intent.getSerializableExtra("officeHour");

However, I'm getting an exception:

android.content.ActivityNotFoundException: Unable to find explicit activity class 
{com.example.app/com.example.app.SixthFragment}; have you declared 
this activity in your AndroidManifest.xml?

I can tell that this means that I need to add an activity declaration to AndroidManifest.xml, but I can't tell what I should add / how this should be formatted. Just as a note, I've tried looking around for pre-existing questions, I still can't tell what exactly to write in my manifest. Thanks!

A Fragment always needs to be hosted by an Activity . You can't transition between fragments by using an Intent . Instead, you could use a FragmentTransaction and pass the data as an argument:

// this code is in SeventhFragment's underlying Activity
Fragment f = new SixthFragment();
Bundle args = new Bundle();
args.putSerializable("officeHour", hour);
f.setArguments(args);

// Execute a transaction to replace SeventhFragment with SixthFragment
FragmentTransaction ft = getFragmentManager().beginTransaction();
// R.id.myfragment needs to be defined in your Activity's layout resource
ft.replace(R.id.myfragment, f);
ft.commit();

You can then retrieve the argument value in the SixthFragment 's onCreateView :

public class SixthFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        OfficeHour add = (OfficeHour) getArguments().getSerializable("officeHour");
        // ...
    }
}

Alternatively, you could embed SixthFragment into its own Activity and use an intent to start it.

You can find more information in the official Fragment documentation .

Intent is used to start new activity. You can't change fragments using Intent. To Change fragments.

Try Below links and code to implement fragments.

fragmnets 1 Fragments in Detail

   FristFragment firstFragmentInstance=new FirstFragment();
    FragmentManager firstFragmentManager=getSupportFragmentManager();
    FragmentTransaction firstFragmentTransaction=firstFragmentManager.beginTransaction();
    firstFragmentTransaction.add(R.id.content_main,firstFragmentInstance,"").commit();

And to replace fragments

   FristFragment firstFragmentInstance=new FirstFragment();
    FragmentManager firstFragmentManager=getSupportFragmentManager();
    FragmentTransaction firstFragmentTransaction=firstFragmentManager.beginTransaction();
    firstFragmentTransaction.replace(R.id.content_main,firstFragmentInstance,"first_fragment_tag").addToBackStack(null).commit()

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