Palanivelraghul
Palanivelraghul

Reputation: 1971

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

Upvotes: 0

Views: 7927

Answers (4)

Harsh Parikh
Harsh Parikh

Reputation: 3845

If you want to go back to the fragment , you can do this :

getFragmentManager().popBackStack();

Upvotes: 0

userAndroid
userAndroid

Reputation: 586

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();
            }
        }

    });


}

Upvotes: 2

Md. Shahadat Sarker
Md. Shahadat Sarker

Reputation: 849

Ok look,

startActivity(intent);

this method name startActivity. So it will start another activity not fragment. You can See & read the fragment: http://developer.android.com/guide/components/fragments.html

Upvotes: 2

Laxmeena
Laxmeena

Reputation: 790

Add fragment to go back to manually to the backstack.

FragmentTransaction transaction = getFragmentManager().beginTransaction();
YourFragmentName myFragment = new YourFragmentName();
transaction.replace(R.id.fragment_container, myFragment);
transaction.addToBackStack(null);
transaction.commit();

And There is several methods here. Just take a look:-

http://developer.android.com/reference/android/app/FragmentManager.html#popBackStack()

Upvotes: 0

Related Questions