简体   繁体   中英

Passing data from one intent to another intent in android

I am trying to pass user_id value from one Intent to another Intent. I know this is very straight forward process and I have done it several times. But with the below code, I am slightly confuse.

I need to pass user_id value from MainActivity class to MenuFragment class. And the actual flow in the code is like this-

MainActivity will call MenuActivity and MenuActivity will call MenuFragment

So that means, I need to pass user_id value from MainActivity to MenuActivity and then from MenuActivity to MenuFragment. Am I right?

If yes, I am able to pass user_id value from MainActivity class to MenuActivity class but I am not sure how to pass same user_id value from MenuActivity class to MenuFragment class. As the way I am calling MenuFragment class is slightly different so I am not sure how to do that in this case.

Can anyone help me with this?

Below is the MainActivity class

public class MainActivity extends MapActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    Bundle bundle = getIntent().getExtras();
    user_id = bundle.getString("USERID");

    setContentView(R.layout.sample);

    findViewById(R.id.sample_button).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 40, getResources()
            .getDisplayMetrics());
        SlideoutActivity.prepare(MainActivity.this, R.id.inner_content, width);

        Bundle bundle = new Bundle();
        bundle.putString("USERID", user_id);
        Intent thesisProject = new Intent(MainActivity.this, MenuActivity.class);
        thesisProject.putExtras(bundle);
        startActivity(thesisProject);

        overridePendingTransition(0, 0);
        }
    });
    }
    }

Below is the MenuActivity class

    public class MenuActivity extends FragmentActivity {

    private SlideoutHelper mSlideoutHelper;
     private String user_id;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        Bundle bundle = getIntent().getExtras();
        user_id = bundle.getString("USERID");

        mSlideoutHelper = new SlideoutHelper(this);
        mSlideoutHelper.activate();
        getSupportFragmentManager().beginTransaction().add(com.korovyansk.android.slideout.R.id.slideout_placeholder, new MenuFragment(), "menu").commit();
        mSlideoutHelper.open();
    }

    ....

}

And below is the MenuFragment class

public class MenuFragment extends ListFragment {

    private String user_id;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, new String[] { " Settings", " Attributes"}));
        getListView().setCacheColorHint(0);
    }

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

        //get selected items
        String selectedValue = (String) getListAdapter().getItem(position);

        if(selectedValue.equals(" Attributes")) {
            Bundle bundle = new Bundle();
            bundle.putString("USERID", user_id);
            Intent thesisProject = new Intent(getActivity(), MatchingInterest.class);
            thesisProject.putExtras(bundle);

            startActivity(thesisProject);
        }
    }
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    user_id = getActivity().getIntent().bundle.getString("USERID");
}

Just append getActivity() function before using getIntent() in your fragment.

new MenuFragment() will call the constructor in MenuFragment so inside MenuFragment have a constructor

MenuFragment(String uid)
{
    this.user_id=uid;
}

and change new MenuFragment() to new MenuFragment(user_id)

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