First of all, I read everything here. https://developer.android.com/guide/components/fragments.html
I have one activity and I want to divide the screen horizontally and add 2 fragments. I know how to add fragments on XML but I don't want that. I want to add them on Java.
So the problem is FragmentManager doesnt work unless my activity extends Fragment. Should I do that on the activity or should I add fragment transaction methods on one of the fragments?
If I extend my activity to Fragment, does it also become a fragment?
If I put the fragmentmanager and fragmenttransaction on one of the fragments how can I make the connection with activity?
this is the activity which I want my fragments displayed on
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class ConversionActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_conversion);
NumpadFragment fragment = new NumpadFragment();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.numpad_layout,fragment);
fragmentTransaction.commit();
}
}
and this is one of the fragments
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class NumpadFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_numpad,container,false);
}
}