0

I WAS WORKING BY THE EXAMPLE YOU JUST GAVE ME AS DUPLICATE, READ THE ENTIRE POST, TY

I'm making a simple app with sliding menu by using the template provided by android studio. And I have to use fragments to switch between items of the sliding menu.

What I wanna do? (check app design at the bottom)

When I click on button pass data it should set the text in the Fragment B to the text that I entered in a Fragment A but without going directly to the Fragment B. So when I press the button pass data then I wanna go to the sliding menu and select item that containts fragment B and then I wanna see the text from Fragment A. Thanks in advance.

public class MainActivity extends AppCompatActivity implements FragmentA.DataPassListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    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(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(MenuItem item) {
            int id = item.getItemId();

            if (id == R.id.nav_camera) {
                // Handle the camera action

                displayFragmentA();
            } else if (id == R.id.nav_gallery) {

                displayFragmentB();

            } else if (id == R.id.nav_slideshow) {

            } else if (id == R.id.nav_manage) {

            } else if (id == R.id.nav_share) {

            } else if (id == R.id.nav_send) {

            }

            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            drawer.closeDrawer(GravityCompat.START);
            return true;
        }
    });
}

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}


@Override
public void passData(String data) {

    FragmentB fragmentB = new FragmentB();
    Bundle args = new Bundle();
    args.putString("data", data);
    fragmentB.setArguments(args);
    //getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, fragmentB).commit();
    //it works with the commented part but I dont wanna go directly to the fragment when I press pass data button


}

public void displayFragmentA() {

    FragmentA frag = new FragmentA();
    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.fragment_container, frag);
    fragmentTransaction.commit();


}

public void displayFragmentB() {

    FragmentB frag = new FragmentB();
    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.fragment_container, frag);
    fragmentTransaction.commit();


}
}

Fragment A code:

public class FragmentA extends Fragment {

DataPassListener mCallback;

public interface DataPassListener {
    public void passData(String data);
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    // Make sure that container activity implement the callback interface
    try {
        mCallback = (DataPassListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString()
                + " must implement DataPassListener");
    }
}


public FragmentA() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_a, container, false);

    final EditText input = (EditText) view.findViewById(R.id.etInputID);

    Button passDataButton = (Button) view.findViewById(R.id.bPassDataID);
    passDataButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


            mCallback.passData(input.getText().toString());

        }
    });


    return view;
}

}

Fragment B code:

public class FragmentB extends Fragment {

TextView showReceivedData;


public FragmentB() {
    // Required empty public constructor
}


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

    View view = inflater.inflate(R.layout.fragment_fragment_b, container, false);

    showReceivedData = (TextView) view.findViewById(R.id.showReceivedData);

    Bundle args = getArguments();
    if (args != null) {

         showReceivedData.setText(args.getString("data"));

    } else {

        Toast.makeText(getActivity(), "didnt get the bundle from mainactivity", Toast.LENGTH_LONG).show();
    }


    return view;
}


}

Design of the app:

Fragment A

Fragment B

Nenco
  • 241
  • 4
  • 13
  • Your question is a bit broad, don't post your entire project, post a minimal example. You will need to save the data somewhere. – cyroxis May 06 '16 at 20:00
  • 1
    Possible duplicate of [How to pass values between Fragments](http://stackoverflow.com/questions/16036572/how-to-pass-values-between-fragments) – NoChinDeluxe May 06 '16 at 20:16
  • NoChinDeluxe I was working by the example you just posted. – Nenco May 06 '16 at 20:18
  • cyroxis is it possible to do without SQLite? cause I'm not exiting the app at all I'm just switching between slider items – Nenco May 06 '16 at 20:27
  • You can use intents, sharedPreferences of EventBus. Take your pick. – Vucko May 06 '16 at 20:47
  • how can I use intents if I have only one activity with 2 fragments inside that activity? I'm using bundle to pass the data from mainactivity to fragment B but without the commented part it doesnt work. I always get the toast inside else ("didnt get the bundle from mainactivity") – Nenco May 06 '16 at 21:39

1 Answers1

3

If these two fragments use same activity,

to set value to fragment :

 getActivity().getIntent().putExtra("key", "value");

To get value from fragment :

getActivity().getIntent().getExtras().getString("key"); 
hkaraoglu
  • 1,345
  • 1
  • 15
  • 34
  • yee, this works, but my problem is that it doesnt show the value when I try to do it without the commented part – Nenco May 06 '16 at 21:14
  • I don't understand what you said exactly. Can you explain this? – hkaraoglu May 06 '16 at 21:18
  • check the method pass data in mainactivity, the part that is commented exactly. If I uncomment this everything works but it goes directly to Fragment B when I press pass data button. And I want to go to the sliding menu and go to the gallery item where is my Fragment B and then inside that gallery item I wanna see the text from Fragment A – Nenco May 06 '16 at 21:23
  • You should store the references of fragment A and B as public variable in Activity class. And the you change text like below : fragmentB.textview.setText("value") – hkaraoglu May 06 '16 at 21:52
  • I stored all the fragments dynamically. So all I have is a fragment container. – Nenco May 06 '16 at 21:57
  • tried that, I'm getting an error then. I will make a screenshot sec – Nenco May 06 '16 at 21:58
  • Whats the error? Put the stacktrace into your post . – hkaraoglu May 06 '16 at 22:01
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/111259/discussion-between-nenco-and-revenge). – Nenco May 06 '16 at 22:14