Click here to Skip to main content
15,888,155 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey guys please need help. A problem with the switch from an activity to fragment in Android Studio.

What I have tried:

So I have used this code: FragmentManager fm = getSupportFragmentManager();
MainFragment fragment = new MainFragment();
fm.beginTransaction().add(R.id.add_activity,fragment).commit();
But it opens a fragment in the same activity while I want the fragment to be opened separately
Posted
Updated 8-Jun-20 22:38pm

1 solution

That is what Android fragment [^] is for,
Quote:
A fragment must always be embedded in an activity and the fragment's lifecycle is directly affected by the host activity's lifecycle.
Otherwise, you have to create a new Activity.
 
Share this answer
 
v2
Comments
Mir Usmanov 17-Mar-17 3:42am    
So, you mean there should be a medium activity between the fragment and that activity from which I want to switch to the fragment?
Peter Leow 17-Mar-17 6:54am    
No. If you want a standalone view, you have to use another Activity. Fragment is out of question. Follow this https://developer.android.com/training/basics/firstapp/starting-activity.html to find out how to start another activity.
Mir Usmanov 17-Mar-17 8:42am    
Thanks a lot Peter

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900