-2

is it possible to pass a value from :FragmentActivity {} to Fragment {}.

Am implemented navigation drawer in my app and the problem is i have categories in the drawer menu. Now i want to pass the category id from public class MainActivity extends FragmentActivity {} to public class Galleries extends Fragment {}.little help is appreciated Edited : guys plz see the links below , i posted my codes there. Tell me how should i pass the Int pos value from OnclickListener in MainActivity to the fragment class Galleries. https://www.dropbox.com/s/pmcdjb3yorvp54w/MainActivity.txt https://www.dropbox.com/s/ivkc15bqz4vp8c5/fragment%20class.txt

sureshbabu
  • 53
  • 1
  • 8
  • You have Bundle as solution when you want to communicate from FragmentActivity to Fragment. FragmentTransaction ft = context.getFragmentManager().beginTransaction(); bundle= new Bundle(); fgmtObject.setArguments(bundle); ft.replace(R.id.contentpage, R.id.fragmentPage); ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); ft.addToBackStack(null); ft.commit(); – Roll no1 Dec 30 '13 at 06:57
  • possible duplictae of http://stackoverflow.com/questions/9343241/passing-data-between-a-fragment-and-its-container-activity – Praveen Sharma Dec 30 '13 at 06:59
  • What have you tried already? We need to know that you're coming here having already put in some effort on your own to find the solution. – DiMono Dec 30 '13 at 07:09
  • @Rollno1. its showing error for context.Error is :context cannot be resolved. – sureshbabu Dec 30 '13 at 07:35
  • @sureshbabu : Where are you getting exception i.e which line is giving you error can you paste that set of code. – Roll no1 Dec 30 '13 at 07:38
  • @ Rollno1 public void onDrawerClosed(View drawerView){ super.onDrawerClosed(drawerView); Galleries obj = new Galleries(); FragmentTransaction tx = getSupportFragmentManager().beginTransaction(); //This line when i add context.getSupportFragmentmanager(). – sureshbabu Dec 30 '13 at 07:59
  • My code is here.. see that . i just wanna pass a value from drawerclosed() to galleries fragment.https://www.dropbox.com/s/xc9y373qnxyzssx/Rick%20Falck.txt – sureshbabu Dec 30 '13 at 10:00

3 Answers3

1

I just fixed it.. Actually no need of constructor in Galleries fragment . I removed constructor and received the value simply by the following code int throwid = getArguments().getInt("ID"); Log.d("BUNDLE==NULL",String.valueOf(throwid)); passed the value by: public void onItemClick(AdapterView parent, View view, final int pos,final long id){

             final Galleries pass= new Galleries();
              Bundle args = new Bundle();
              args.putInt("ID", pos);
              pass.setArguments(args);
          if (pass!=null){ 
             Log.d("bundle","has value");
           }

                    drawer.setDrawerListener( new DrawerLayout.SimpleDrawerListener(){
                             @Override 
                             public void onDrawerClosed(View drawerView){
                                     super.onDrawerClosed(drawerView);

                                     //FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
                                     //tx.replace(R.id.main,Fragment.instantiate(MainActivity.this, fragments[0]));
                                     FragmentTransaction tx = getSupportFragmentManager().beginTransaction(); tx.replace(R.id.main, pass); tx.commit();
sureshbabu
  • 53
  • 1
  • 8
0

From FragmentActivity you can send data to Fragment with intent as:

FragmentTransaction ftran = context.getFragmentManager().beginTransaction();
Bundle bundle=new Bundle();
bundle.putInt("Id", <your id here>);
Galleries fragobj=new Galleries ();
fragobj.setArguments(bundle);
ftran.replace(R.id.firstpage, R.id.galleryfragmentPage); 
ftran.commit();

and get id to next Fragment:

Bundle bundle = this.getArguments();
int myID = bundle.getInt("Id", 0);

I do not know what logic you have implmented. Try out as below:

   FragmentTransaction tx = getSupportFragmentManager().beginTransaction(); 
    Bundle bundle = new Bundle();
    String check="checking...";
bundle.putString("id_cat", check);
Galleries obj =new Galleries();
     obj.setArguments(bundle);
    Log.d("BUNDLE", bundle.toString());
    tx.replace(R.id.main, obj);
tx.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                                    // int position =5;
tx.commit();
GrIsHu
  • 29,068
  • 10
  • 64
  • 102
  • This is not working ...check my code and if possible tell me a good solution.https://www.dropbox.com/s/xc9y373qnxyzssx/Rick%20Falck.txt – sureshbabu Dec 30 '13 at 10:01
0

Other than passing data in the Intent Bundle, there is a way to get direct access to a Fragment.

In a FragmentActivity, you use FragmentManager to add your Fragment.

You can also use FragmentManager to get your Fragment (using findById()). After getting it and casting it to your classname, you can then easily call public methods in your Fragment.

Example:

FragmentManager fm = getSupportFragmentManager();
MyFragment fragment = (MyFragment) fm.findFragmentById(R.id.fragmentContainer);

fragment.updateData(..);

EDIT

To get data from the passed intent do something like this:

Intent in = getActivity().getIntent();
String str = in.getStringExtra(EXTRA_STRING_DATA);

The getArguments() is for a Bundle attached to the Fragment, not the one passed in the Intent.

EDIT2

You can't create the Fragment the way you are doing it.

In your activity do this:

FragmentTransaction tx = getSupportFragmentManager().beginTransaction();

// int position =5;
String check="checking...";
Galleries fragment = Galleries,newInstance(check);
tx.replace(R.id.main, Fragment.instantiate(MainActivity.this, fragment));
tx.commit();

Create a static method in Galleries:

public static Fragment newInstance(String check) {
    Bundle args = new Bundle();
    args.putString("id_cat", check);
    Galleries fragment = new Galleries();
    fragment.setArguments(args);
    return fragment;
}

Now you can do getArguments() in onCreateView().

Rick Falck
  • 1,778
  • 3
  • 15
  • 19
  • It will be more helpful if u elobrate your answer with example or else just give any link , that has an good example for that. Anyway thank u for this – sureshbabu Dec 30 '13 at 07:54
  • Not much to elaborate here. You updated your question after I posted this. Maybe it doesn't apply to your situation. My answer is how you can send data to a fragment that is already displayed (like your Activity gets gets info from somewhere else and needs to send it on). – Rick Falck Dec 30 '13 at 08:00
  • ok. thanx . I will update my MainActivity code. so that u can understand my situation and help me.. – sureshbabu Dec 30 '13 at 08:06
  • See the edit I made to my answer. Let me know if that helps. – Rick Falck Dec 30 '13 at 08:09
  • am trying to put my full code .. it s really hard. shall i mail it to you – sureshbabu Dec 30 '13 at 08:46
  • i have created fragment. its working.. but am not able to pass value from mainactivity to galleries(fragment) – sureshbabu Dec 30 '13 at 09:56
  • I tried. Others have down-voted your question, because it shows a lack of understanding the basics. This is apparently so. The **Big Nerd Ranch Android book** is what I read to start Android programming. It covers a lot and is up to date. – Rick Falck Dec 30 '13 at 21:23
  • Oh! Its k . Thanx for your help and book. I will read the book for sure. – sureshbabu Dec 31 '13 at 05:28