简体   繁体   中英

Android how to send data from Activity to Fragment?

I want to pass data from my Activity to a Fragment . I have no idea how to do it. I've seen many solutions but no one of them did really work.

I just want to pass a simple String to the Fragment .

I have tried it this way:

public class PhotoActivty extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photo);
        if (null == savedInstanceState) {
            Bundle bundle = new Bundle();
            String myMessage = "Stackoverflow is cool!";
            bundle.putString("message", myMessage );
            BasicFragment fragInfo = new BasicFragment();
            fragInfo.setArguments(bundle);
            android.app.FragmentTransaction transaction = getFragmentManager().beginTransaction();
            transaction.replace(R.id.photo_frame, fragInfo);
            transaction.commit();
        }
    }
}

What is the error there? I call it this way:

String myValue = this.getArguments().getString("message"); 

In the onCreateView in the Fragment

Bundle bundle = new Bundle();
String myMessage = "Stackoverflow is cool!";
bundle.putString("message", myMessage );
Fragment fragInfo = new BasicFragment();
fragInfo.setArguments(bundle);
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.photo_frame, fragInfo);
transaction.commit();

try this inside your if

I usually use a static factory pattern:

public class MyFragment extends Fragment {
    public static MyFragment newInstance(int index) {
        MyFragment f = new MyFragment();
        Bundle args = new Bundle();
        args.putInt("index", index);
        f.setArguments(args);
        return f;
    }
}

When you create the fragment in your Activity:

Fragment MyFragment = MyFragment.newInstance(5);

Alex Lockwood has a good rundown on why this is a preferred design pattern: http://www.androiddesignpatterns.com/2012/05/using-newinstance-to-instantiate.html

from activity:

Bundle bundleObject = new Bundle();
bundleObject.putString("data", " Send From Activity");
/*set Fragmentclass Arguments*/
Fragment fragmentobject = new Fragment();
fragmentobject .setArguments(bundleObject );

From Fragment You receive this way:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
String stringText = getArguments().getString("data");    
return inflater.inflate(R.layout.fragment, container, false);

Not sure if this is the real cause of the problem, but you have to use getSupportFragmentManager() instead of getFragmentManager() inside AppCompatActivity . Also, classes such as Fragment and FragmentTransaction should come from support.v4 package.

Remove if (null == savedInstanceState) in your activity. If savedInstanceStatenull == null then BasicFragment will not replace your FrameLayout for R.id.photo_frame .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM