Monday, August 20, 2018

How to start a fragment Insted of opening new Activity, OnClick its opening new Activity, Is there any way I can open fragment

How to start a fragment Insted of opening new Activity, OnClick its opening new Activity, Is there any way I can open fragment.

@Override
protected void onPostExecute(List result) {
    super.onPostExecute(result);

  final MovieAdapter adapter = new MovieAdapter( getActivity().getApplicationContext(), R.layout.rownew, result );
        ////  getApplicationContext()   // getActivity is added by me
        lvMovies.setAdapter(adapter);

        //set data to list
        lvMovies.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView parent, View view, int position, long id) {
              //  Toast.makeText(getActivity().getBaseContext(),parent.getItemIdAtPosition(position)+" is selected",Toast.LENGTH_LONG).show();

                MovieModel movieModel = (MovieModel) adapter.getItem(position);

                Intent intent = new Intent("sanjay.apackage.torcente.com.torcentemotors.productdesc");
                intent.putExtra("productimage", movieModel.getProduct_image());
                //sanjay //
                intent.putExtra("productname",movieModel.getProduct_name());
                intent.putExtra("productprice", movieModel.getProduct_price());
                intent.putExtra("productcolor", movieModel.getProduct_color());
                intent.putExtra("originalprice", movieModel.getOriginal_price());
                intent.putExtra("appdesc", movieModel.getApp_desc());

                startActivity(intent);

                /*
                Fragment fragment_productdesc = new fragment_productdesc();
                FragmentTransaction transaction = getFragmentManager().beginTransaction();
                transaction.replace(R.id.main_container, fragment_productdesc ); // give your fragment container id in first parameter
                transaction.addToBackStack(null);  // if written, this transaction will be added to backstack
                transaction.commit();  */
            }
        });
    }

Solved

I think you want to send data from activity to fragment so please try this method to send data from your activity to method.

Create a custom fragment extending fragment put arguments and initialize.

public class MyFragment extends Fragment {
private View rootView;
private int type;

public MyFragment() {
    // Required empty public constructor
}

public static MyFragment newInstance(int type, Object object) {
    MyFragment fragment = new MyFragment();
    Bundle args = new Bundle();
    args.putInt("type", type);
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        type = getArguments().getInt(Key.TYPE);
    }
}

@Override
public View onCreateView(
        LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_slider, container,false);
    return rootView;
}

No comments:

Post a Comment