palanivel raghul palanivel raghul - 1 month ago 18
Android Question

go back from activity to fragment in android

I have a two class named Stock Details extends Fragment and another one is Stock info extends activity,
when I was trying to go back to my Stock details pages from Stock info pages it shows "Unfortunately,Application has stopped",but in my program I use

Intent intent = new Intent(this,StockDetails.class);
startActivity(intent);
finish();


for go back to fragment. but it does not work. help me to solve this problem
I am the beginner for android.
kindly help me to go back from an Activity to fragment

Answer

you do not need to start activity for StoreDetail as it is a subclass of Fragment not Activity.This is the reason that your app is crashed.

Now moving to your question if you want to go back to the fragment from the activty : Stock INFO you just need to call finish() it will finish the current activity(Stock Info) and the fragment which is in background will be resumed.I had same problem and solved by this way .This is the onCreate Method of Activity(in your case it is for StockInfo class).Have a look:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    this.setmCallBack(new IResultCallback() {
        @Override
        public void result(Result lastResult) {

            if (lastResult!=null) {
                finish();// by this line I have killed the current activity

            }
            else
            {

            Toast.makeText(getActivity(), "NotScan: ", Toast.LENGTH_SHORT).show();
            }
        }

    });


}