-1

now i'm in fragment activity. and now i wanna go to Activity from fragment. Here is my code tell me ?

public class People extends BaseFragment implements OnClickListener {
    static String name;
    ListView listview;
    TextView et;
    public static String username;
    static public List<SuccessStoreyItem> success_list3 = new ArrayList<SuccessStoreyItem>();
    public static int people_index ;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        // TODO Auto-generated method stub

        View view= inflater.inflate(R.layout.home,null);
        ParserResult pr = new ParserResult();
        Button b=(Button) view.findViewById(R.id.home1);
        listview = (ListView)view.findViewById(R.id.lv_home);

        et=(TextView) view.findViewById(R.id.editText1);
        b.setOnClickListener(this);

        if(ParserResult.success_list2.size() != 0){
            success_list3 = ParserResult.success_list2;
        }

        listview.setAdapter(new Adapters(success_list3 ,MainActivity.mainactivity_context));
        ParserResult.success_list2 = new ArrayList<SuccessStoreyItem>();


        listview.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // TODO Auto-generated method stub
//              Toast.makeText(getActivity(), "fuck you", Toast.LENGTH_LONG).show();
                Profile frg3 = new Profile();
                Bundle b3 = new Bundle();
                username = "";
                username = success_list3.get(position).getStorey_id();
                Intent i = new Intent(MainActivity.mainactivity_context,Profile_otheruser.class);
                startActivity(i);
//              b3.putString("username", username);
//              b3.putInt("index_people", position);
                people_index = position;
                frg3.setArguments(b3);
//              ((MainActivity)getActivity()).launchNewFragment(frg3, R.id.tab2);
//              Toast.makeText(getActivity(), "Name :"+name ,1).show();
            }
        });



        return view;
    }
    public void onListItemClick(ListView l, View v, int position, long id) {
        //Do your stuff..

    }
    @Override
    public void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        setTitle(R.string.Home);
    }
    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        name=et.getText().toString();
        Bundle bundle = new Bundle();
        bundle.putString("Name", name);

    Toast.makeText(getActivity(), "Name :"+name ,1).show();


        //((MainActivity)getActivity()).launchNewFragment(new Home2(),R.id.tab1);
    }


}

text is: tell me that my that my method (context,activity.class) , start activity(i) class method is right?

  • 1
    What problem are you facing? – Apoorv Aug 28 '14 at 14:16
  • You possibly want to know what Activity and Fragment in Android are, how they relate, and incorporate with each other before asking such questions. http://developer.android.com/guide/components/fragments.html – Alexander Zhak Aug 28 '14 at 14:39
  • you possibly want to execute code on the activity from the fragment which is many times over duplicated question here. But I've answered it below. – danny117 Aug 28 '14 at 18:45

1 Answers1

0

Fragment contains an interface. Activity implements the interface. This way the fragment can be used with any number of activities that implement the interface.

public class SettingMap extends Fragment implements OnCheckedChangeListener,
    android.widget.CompoundButton.OnCheckedChangeListener {



public interface BestRidesSettingsDialogListener {
    // change the map type
    void onMapSettingsChange(int mapType);
}

somewhere in the fragment safely check the activity has implemented the interface and then execute code on the activity

    Activity a = getActivity();
    BestRidesSettingsDialogListener activity = (BestRidesSettingsDialogListener.class.isAssignableFrom(a
            .getClass())) ? (BestRidesSettingsDialogListener) a : null;
    if (activity != null) {
        activity.onMapSettingsChange(mapType);
    }

Activity implements the interface

public class KmlReader extends ActionBarActivity implements
    BestRidesFollowDialogListener {


@Override
public void onMapSettingsChange(int mapType) {
    if (mMap != null) {
        mMap.setMapType(mapType);
    }
}
danny117
  • 5,581
  • 1
  • 26
  • 35
  • do u know about fragmentpager , pageradapter? – Faisal Ashraf May 15 '15 at 16:20
  • Yes I have one tip for the fragmentpager adapter for onActivityResult. http://stackoverflow.com/questions/6147884/onactivityresult-not-being-called-in-fragment/27087414#27087414 – danny117 May 21 '15 at 12:43