ActionBar Up button: go to the previous operation with the previous fragment

I need your help regarding my application stream.

MainActivity (with Navigation Drawer)
-- Fragment A
-- Fragment B
-- Fragment C (articles list view)

ArticleActivity
-- Fragment D (article detail view)

Fragment C ( MainActivity) displays a list of elements ( ListView). Selecting an element leads to a fragment D (descriptor ArticleActivity), which represents this element in more detail.

Fragment D displays the Up button, which should allow the user to return to the previous screen (detailed view). The problem is that when you press the Up button, the previous activity is displayed, but not the previous activated fragment (the defaut ( A) fragment is displayed instead ).

My current code is:

public class FragmentC extends Fragment {
    public static FragmentC newInstance() {
        FragmentC fragment = new FragmentC();
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_courses, container, false);

        ...
        // Somewhere in my code I have a onClickListener to launch the detail view activity
        setOnClickListener(new OnCardClickListener() {
            @Override
            public void onClick(Card card, View view) {
                Intent i = new Intent(getActivity(), ArticleActivity.class);
                i.putExtra("articleIndex", mArticle.getId());
                startActivity(i);
            }
        });
        ...

        return view;
    }
}

public class ArticleActivity extends Activity {
    private long mArticleIndex;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_course);

        mArticleIndex = getIntent().getExtras().getLong("articleIndex", 0);

        // Set up action bar:
        // Specify that the Home button should show an "Up" caret, indicating that touching the
        // button will take the user one step up in the application hierarchy.
        final ActionBar actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);

        // Place an ArticleFragment as our content pane
        ArticleFragment fragment = ArticleFragment.newInstance(mArticleIndex);
        getFragmentManager().beginTransaction().replace(R.id.container, fragment).commit();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                // This is called when the Home (Up) button is pressed in the action bar.
                Intent upIntent = new Intent(this, MainActivity.class);
                upIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(upIntent);
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

, , Intent. i.putExtra("displayFragment", FragmentD), MainActivity FragmentManager, . , .

, ?

?

+1

All Articles