0

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);
    }
}
gunesevitan
  • 882
  • 10
  • 25
  • What do you mean by `So the problem is FragmentManager doesnt work unless my activity extends Fragment` ? – ρяσѕρєя K Jun 07 '17 at 07:54
  • For adding to Fragment using Code just create two container layouts in `activity_conversion.xml` and add both Fragments using `fragmentTransaction.add(R.id.,fragment);` and `fragmentTransaction.add(R.id.,fragment);` – ρяσѕρєя K Jun 07 '17 at 07:55
  • You want to use `getSupportFragmentManager()` instead of `getFragmentManager()`. – Mike M. Jun 07 '17 at 07:57
  • When I hover the mouse on FragmentManager. I get imcompatible types error. It says I need to import android.support.v4.app.FragmentManager not android.app.FragmentManager. But I have the first one. If I extend to Fragment that error disappears. – gunesevitan Jun 07 '17 at 08:03
  • @MikeM. app crashes when I use it. – gunesevitan Jun 07 '17 at 08:12
  • That would be a different problem. If it compiles, then you've got the types right. Look at the logcat for the stack trace from the crash. http://stackoverflow.com/questions/23353173 – Mike M. Jun 07 '17 at 08:14
  • @MikeM. I get no view found error for my fragment on logcat but I have it? – gunesevitan Jun 07 '17 at 08:31
  • The ID you pass a `FragmentTransaction` is for the `ViewGroup` that the `Fragment` will go into, not one in the `Fragment`'s layout. Apparently `numpad_layout` is not in the `activity_conversion` layout. – Mike M. Jun 07 '17 at 08:32

1 Answers1

1

Some suggestions:

  • Use fragmentTransaction.replace (unless you need your fragments to be added to stack)
  • As containter for your fragment, use FrameLayout
  • Don't create new fragment by "new" operator. Add newInstance() method instead and create new fragment instance by calling that methrod. Also, implement interaction between your fragment and activity. Tip: use Android Studio wizard to add fragment to your project. Android Studio will generate skeleton with all needed methods already included
  • You definitely need to use getSupportFragmentManager (because your fragment is android.support.v4.app.Fragment)
  • DEBUG! - if you say "my app is crashing", it's useless. Post your logcat/exception/whatever to show crash reason.
user1209216
  • 7,404
  • 12
  • 60
  • 123