简体   繁体   中英

Passing data from Fragment to FragmentActivity not functioning

I'm not sure why the code I have set up isn't working correctly.

I am trying to start an activity and pass some data when the intent is called to open the activity.

For some reason, this isn't working as planned and when I click my list item to open up an activity, just a blank black screen comes up, this shouldn't happen and my data passing isn't being passed correctly.

here's my code, hopefully someone can guide me in the correct direction.

Calling code:

    @Override
public void onListItemClick(ListView l, View v, int position, long id) {

    if (position == 0) {

        String msg = "First";
        Intent i = new Intent(getActivity(), ImageGridActivity.class);
        i.putExtra("1", msg);
        startActivity(i);
    }
    if (position == 1) {

        String msg = "Second";
        Intent i = new Intent(getActivity(), ImageGridActivity.class);
        i.putExtra("2", msg);
        startActivity(i);
    }

}

Handling class:

public class ImageGridActivity extends FragmentActivity {

private static final String Abstract = "Abstract";
private static final String Animal = "Animal";


@Override
protected void onCreate(Bundle savedInstanceState) {
    if (BuildConfig.DEBUG) {
        Utils.enableStrictMode();
    }
    super.onCreate(savedInstanceState);

    Bundle extras = getIntent().getExtras();
    String msg = extras.getString("KeyMessage");

    if (msg == "1") {

        if (getSupportFragmentManager().findFragmentByTag(Abstract) == null) {
            final FragmentTransaction ft = getSupportFragmentManager()
                    .beginTransaction();
            ft.add(android.R.id.content, new AbstractGridFragment(),
                    Abstract);
            ft.commit();

        }

        if (msg == "2") {

        }
        if (getSupportFragmentManager().findFragmentByTag(Animal) == null) {
            final FragmentTransaction ft = getSupportFragmentManager()
                    .beginTransaction();
            ft.add(android.R.id.content, new AnimalGridFragment(), Animal);
            ft.commit();
        }

    }

}
}

Keys don't match

    String msg = extras.getString("KeyMessage");

While your keys are "1" and "2"

Check the docs

http://developer.android.com/reference/android/content/Intent.html

public Intent putExtra (String name, String value)

Add extended data to the intent. The name must include a package prefix, for example the app com.android.contacts would use names like "com.android.contacts.ShowAll".

Parameters

name    The name of the extra data, with package prefix.
value   The String data value.

Returns

Returns the same Intent object, for chaining multiple calls into a single statement.

You probably want

   Intent i = new Intent(getActivity(), ImageGridActivity.class);
   i.putExtra("key", "1"); // "key" is the key and "1" is the value
   startActivity(i);

and

   Intent i = new Intent(getActivity(), ImageGridActivity.class);
   i.putExtra("key", "2");
   startActivity(i);

Then

   String msg = extras.getString("key");

Also use .equals or .equalsIgnoreCase to compare strings

   if (msg.equals("1")) {

You wrong in key send/reception. You should try like this:

Instante your String msg

String msg;

if

msg = "1";
i.putExtra("myKey", msg);  

else

msg = "2";
i.putExtra("myKey", msg);  

So:

String msg = extras.getString("myKey");
if("1".equals(msg)) // so...
else if("2".equals(msg)) // so...  

Hope this help.

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