Error passing a variable from an Activity to a fragment

1

When I pass a variable from the activity of the navigation drawer to a fragment, it gives me an error of null when retrieving that variable. I'm going to put only the necessary code. This activity receives variables from the main and puts them in a bundle to send them to the fragment. This is the onCreate of the Navigation Drawer activity:

    public class NavigationDrawerActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, InicioFragment.OnFragmentInteractionListener, PerfilFragment.OnFragmentInteractionListener {
    Alumno alum;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_navigation_drawer);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
        navigationView.setItemIconTintList(null);
        Fragment fragment = new InicioFragment();
        getSupportFragmentManager().beginTransaction().add(R.id.content_drawer, fragment).commit();

        String tipo = getIntent().getExtras().getString("Tipo");
        Fragment perfil = new PerfilFragment();
        Bundle bundle = new Bundle();

        alum = (Alumno) getIntent().getExtras().getSerializable("User");

        try {
            Picasso.with(getApplicationContext()).load(alum.getFoto_perfil()).error(R.drawable.user).into(miImageViewDrawerFoto);
        }catch(IllegalArgumentException e){
            miImageViewDrawerFoto.setImageResource(R.drawable.user);
        }

        bundle.putSerializable("User", alum);
        bundle.putString("Tipo", tipo);

        perfil.setArguments(bundle);
    }
}

And this is the fragment onCreate method where I retrieve the variable. The null error gives me on the String line type = getArguments (). GetString ("Type");

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        tipo = getArguments().getString("Tipo");
    }

Thanks.

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="10dp"
    android:layout_margin="0dp"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="com.example.pepe.proy.Fragments.PerfilFragment">    
<TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/dni"
                style="@style/LabelsMedium"
                />
</FrameLayout>
    
asked by PacoPepe 10.05.2018 в 18:57
source

2 answers

2

If you want to send a data to a Fragment you do not need to start a Activity with:

startActivity(i);

This is done to send data between Activities:

Send data between activities

What you must do is a transaction of Fragment , add to your FrameLayout an id contenedor :

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="10dp"
    android:layout_margin="0dp"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="com.example.pepe.proy.Fragments.PerfilFragment"
    android:id="@+id/contenedor"> 

and this would be the code:

        String tipo = "Alumno";
        Intent i = new Intent(MainActivity.this, NavigationDrawerActivity.class);
        PerfilFragment perfiles = new PerfilFragment();
        Bundle bundle = new Bundle();
        bundle.putString("Tipo", tipo);                  
        perfiles.setArguments(bundle);    
        startActivity(i);

        //Realiza la transacción del Fragment
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction ft = fragmentManager.beginTransaction();
        ft.add(R.id.contenedor, perfiles);
        ft.commit();
    
answered by 10.05.2018 в 19:39
2

You should read the official documentation on the Fragment so you have an idea of how they work. Unlike the Activity that start with Intent , the Fragment should be created with Transactions and such transactions should be handled to define the behavior of it.

An example of how to add a Fragment to a container is as follows (this code must go in MainActivity which is where you define the container for the Fragment):

string tipo = "Alumno";
string tag = "Perfil";

PerfilFragment perfil = new PerfilFragment();
Bundle arguments = new Bundle();
arguments.putString("Tipo", tipo);
perfil.setArguments(arguments);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.add(R.id.fragment, perfil, tag);
ft.commit();

Note that R.id.fragment must be the id the view that will contain the Fragment , the most recommended is a FrameLayout linked to the Activity . That is, it must be in the Layout that loads the Activity .

In your case, for example, if the layout of your MainActivity is called: activity_main.axml , that is, in the OnCreate you made setContentView(R.layout.activity_main); you must add the following to your axml :

. . .

<FrameLayout 
    android:id="@+id/fragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>  

. . .

My recommendation is that you read the documentation so that you understand how it works and what the difference is:

answered by 10.05.2018 в 19:43