2

I have "home_fragment.java" class file in my project which i need to be launched when the app starts. but i am only able to add 'activities' as launch default,not fragments. please help me to add a 'fragment' as a launch activity. i am new to android ,thank you.

this is my home_fragment.java

public class home_fragment extends Fragment {
    View myView;
    Button more;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {

        View myview = inflater.inflate(R.layout.home_layout,container, false);
        Button button = (Button) myview.findViewById(R.id.button5);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // do something
            }
        });
        return myview;
    }
}
Komal12
  • 3,340
  • 4
  • 16
  • 25
Ben Sahil
  • 21
  • 2
  • you can't use fragment as launching activity because fragment is not activity, it's just similar to it or we can say that it can be small part of activity. you can take one activity and set your fragment to it for complete your desired work. – Jaydip Kalkani Jan 25 '18 at 08:33
  • 1
    probable duplicate question - https://stackoverflow.com/questions/36063704/how-to-launch-activity-and-show-specific-fragment – Farwa Jan 25 '18 at 08:37

2 Answers2

0

you have to add the fragment into the activity and launch this activity.

it is not possible for fragment to be launched independently.

in empty activity add this xml

<fragment android:name="com.company.appName.fragments.FirstFragment"
        android:id="@+id/fragment_place"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

and in activity onCreate method do this

Fragment fr = new FirstFragment();
fr.setArguments(args);
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.fragment_place, fr);
fragmentTransaction.commit();

hope this will help you.

Nouman Ch
  • 4,023
  • 4
  • 29
  • 42
  • sir please describe how to add fragment to an activity ,mine is a navigation drawer app. – Ben Sahil Jan 25 '18 at 08:34
  • do you need any further explanation? – Nouman Ch Jan 25 '18 at 08:36
  • @BenSahil If your question is "how to add fragment to an activity" here is your answer - https://stackoverflow.com/questions/5159982/how-do-i-add-a-fragment-to-an-activity-with-a-programmatically-created-content-v – Farwa Jan 25 '18 at 08:47
  • comment out this line fr.setArguments(args); if you want to pass any data to fragment from activity them you pass it using setArgumetns method. in your case comment it out. – Nouman Ch Jan 25 '18 at 09:01
  • fr.setArguments(args); – Ben Sahil Jan 25 '18 at 09:41
0

Fragment must be launched from Activity. You can do it by launching your fragment from your activity onCreate() method like this:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getFragmentManager().beginTransaction().replace(android.R.id.content, new MyFragment()).commit();
    }  
Dumbo
  • 1,630
  • 18
  • 33