├── FirstFragment.png
├── Second Fragment.png
├── FragmentBackStack
├── ic_launcher-web.png
├── libs
│ └── android-support-v4.jar
├── res
│ ├── drawable-hdpi
│ │ └── ic_launcher.png
│ ├── drawable-mdpi
│ │ └── ic_launcher.png
│ ├── drawable-xhdpi
│ │ └── ic_launcher.png
│ ├── drawable-xxhdpi
│ │ └── ic_launcher.png
│ ├── values
│ │ ├── dimens.xml
│ │ ├── styles.xml
│ │ └── strings.xml
│ ├── layout
│ │ ├── activity_main.xml
│ │ ├── home_fragment.xml
│ │ └── result_fragment.xml
│ └── values-w820dp
│ │ └── dimens.xml
├── .classpath
├── project.properties
├── proguard-project.txt
├── .project
├── AndroidManifest.xml
└── src
│ └── com
│ └── chintanrathod
│ └── fragmentbackstack
│ ├── ResultListFragment.java
│ ├── HomeListFragment.java
│ └── MainActivity.java
├── .travis.yml
└── README.md
/FirstFragment.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ChintanRathod/Fragment-Back-Stack/HEAD/FirstFragment.png
--------------------------------------------------------------------------------
/Second Fragment.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ChintanRathod/Fragment-Back-Stack/HEAD/Second Fragment.png
--------------------------------------------------------------------------------
/FragmentBackStack/ic_launcher-web.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ChintanRathod/Fragment-Back-Stack/HEAD/FragmentBackStack/ic_launcher-web.png
--------------------------------------------------------------------------------
/FragmentBackStack/libs/android-support-v4.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ChintanRathod/Fragment-Back-Stack/HEAD/FragmentBackStack/libs/android-support-v4.jar
--------------------------------------------------------------------------------
/FragmentBackStack/res/drawable-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ChintanRathod/Fragment-Back-Stack/HEAD/FragmentBackStack/res/drawable-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/FragmentBackStack/res/drawable-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ChintanRathod/Fragment-Back-Stack/HEAD/FragmentBackStack/res/drawable-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/FragmentBackStack/res/drawable-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ChintanRathod/Fragment-Back-Stack/HEAD/FragmentBackStack/res/drawable-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/FragmentBackStack/res/drawable-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ChintanRathod/Fragment-Back-Stack/HEAD/FragmentBackStack/res/drawable-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/FragmentBackStack/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | 16dp
5 | 16dp
6 |
7 |
8 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: android
2 | android:
3 | components:
4 | # Uncomment the lines below if you want to
5 | # use the latest revision of Android SDK Tools
6 | # - platform-tools
7 | # - tools
8 |
9 | # The BuildTools version used by your project
10 | # - build-tools-19.1.0
11 |
12 | # The SDK version used to compile your project
13 | - android-14
14 |
--------------------------------------------------------------------------------
/FragmentBackStack/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
8 |
9 |
--------------------------------------------------------------------------------
/FragmentBackStack/res/values-w820dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
8 | 64dp
9 |
10 |
11 |
--------------------------------------------------------------------------------
/FragmentBackStack/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/FragmentBackStack/project.properties:
--------------------------------------------------------------------------------
1 | # This file is automatically generated by Android Tools.
2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED!
3 | #
4 | # This file must be checked in Version Control Systems.
5 | #
6 | # To customize properties used by the Ant build system edit
7 | # "ant.properties", and override values to adapt the script to your
8 | # project structure.
9 | #
10 | # To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
11 | #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
12 |
13 | # Project target.
14 | target=android-19
15 |
--------------------------------------------------------------------------------
/FragmentBackStack/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
7 |
14 |
15 |
16 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/FragmentBackStack/proguard-project.txt:
--------------------------------------------------------------------------------
1 | # To enable ProGuard in your project, edit project.properties
2 | # to define the proguard.config property as described in that file.
3 | #
4 | # Add project specific ProGuard rules here.
5 | # By default, the flags in this file are appended to flags specified
6 | # in ${sdk.dir}/tools/proguard/proguard-android.txt
7 | # You can edit the include path and order by changing the ProGuard
8 | # include property in project.properties.
9 | #
10 | # For more details, see
11 | # http://developer.android.com/guide/developing/tools/proguard.html
12 |
13 | # Add any project specific keep options here:
14 |
15 | # If your project uses WebView with JS, uncomment the following
16 | # and specify the fully qualified class name to the JavaScript interface
17 | # class:
18 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
19 | # public *;
20 | #}
21 |
--------------------------------------------------------------------------------
/FragmentBackStack/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | FragmentBackStack
5 | Hello world!
6 | Settings
7 |
8 |
9 | - 1
10 | - 2
11 | - 3
12 | - 4
13 | - 5
14 | - 6
15 | - 7
16 | - 8
17 |
18 |
19 | - 11
20 | - 22
21 | - 33
22 | - 44
23 | - 55
24 | - 66
25 | - 77
26 | - 88
27 |
28 |
29 |
--------------------------------------------------------------------------------
/FragmentBackStack/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | FragmentBackStack
4 |
5 |
6 |
7 |
8 |
9 | com.android.ide.eclipse.adt.ResourceManagerBuilder
10 |
11 |
12 |
13 |
14 | com.android.ide.eclipse.adt.PreCompilerBuilder
15 |
16 |
17 |
18 |
19 | org.eclipse.jdt.core.javabuilder
20 |
21 |
22 |
23 |
24 | com.android.ide.eclipse.adt.ApkBuilder
25 |
26 |
27 |
28 |
29 |
30 | com.android.ide.eclipse.adt.AndroidNature
31 | org.eclipse.jdt.core.javanature
32 |
33 |
34 |
--------------------------------------------------------------------------------
/FragmentBackStack/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
10 |
11 |
16 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
--------------------------------------------------------------------------------
/FragmentBackStack/res/layout/home_fragment.xml:
--------------------------------------------------------------------------------
1 |
10 |
11 |
18 |
19 |
29 |
30 |
--------------------------------------------------------------------------------
/FragmentBackStack/res/layout/result_fragment.xml:
--------------------------------------------------------------------------------
1 |
10 |
11 |
18 |
19 |
29 |
30 |
--------------------------------------------------------------------------------
/FragmentBackStack/src/com/chintanrathod/fragmentbackstack/ResultListFragment.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2014 The Android Open Source Project
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.chintanrathod.fragmentbackstack;
18 |
19 | import android.os.Bundle;
20 | import android.support.v4.app.Fragment;
21 | import android.view.LayoutInflater;
22 | import android.view.View;
23 | import android.view.ViewGroup;
24 | import android.widget.AdapterView;
25 | import android.widget.ListAdapter;
26 | import android.widget.ListView;
27 | import android.widget.Toast;
28 |
29 | public class ResultListFragment extends Fragment {
30 |
31 | View view;
32 |
33 | ListView listView;
34 |
35 | private ListAdapter adapter;
36 |
37 | @Override
38 | public View onCreateView(LayoutInflater inflater, ViewGroup container,
39 | Bundle savedInstanceState) {
40 |
41 | view = inflater.inflate(R.layout.result_fragment, null);
42 |
43 | initViews();
44 |
45 | return view;
46 | }
47 |
48 | private void initViews() {
49 | listView = (ListView) view.findViewById(R.id.listView1);
50 |
51 | adapter = listView.getAdapter();
52 |
53 | listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
54 | @Override
55 | public void onItemClick(AdapterView> adapterView, View view,
56 | int position, long id) {
57 | Toast.makeText(getActivity(),
58 | adapter.getItem(position).toString(), Toast.LENGTH_LONG)
59 | .show();
60 | }
61 | });
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/FragmentBackStack/src/com/chintanrathod/fragmentbackstack/HomeListFragment.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2014 The Android Open Source Project
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.chintanrathod.fragmentbackstack;
18 |
19 | import android.os.Bundle;
20 | import android.support.v4.app.Fragment;
21 | import android.view.LayoutInflater;
22 | import android.view.View;
23 | import android.view.ViewGroup;
24 | import android.widget.AdapterView;
25 | import android.widget.ListAdapter;
26 | import android.widget.ListView;
27 |
28 | public class HomeListFragment extends Fragment {
29 |
30 | View view;
31 | ListView listView;
32 | private ListAdapter adapter;
33 | private MyListFragmentListener listener;
34 |
35 | public interface MyListFragmentListener {
36 | public void onItemClickedListener(String valueClicked);
37 | }
38 |
39 | @Override
40 | public View onCreateView(LayoutInflater inflater, ViewGroup container,
41 | Bundle savedInstanceState) {
42 |
43 | view = inflater.inflate(R.layout.home_fragment, null);
44 |
45 | initViews();
46 |
47 | return view;
48 | }
49 |
50 | public void registerForListener(MyListFragmentListener listener) {
51 | this.listener = listener;
52 | }
53 |
54 | private void initViews() {
55 | listView = (ListView) view.findViewById(R.id.listView1);
56 |
57 | adapter = listView.getAdapter();
58 |
59 | listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
60 | @Override
61 | public void onItemClick(AdapterView> adapterView, View view,
62 | int position, long id) {
63 |
64 | if (listener != null) {
65 | listener.onItemClickedListener(adapter.getItem(position)
66 | .toString());
67 | } else {
68 | throw new IllegalArgumentException("Please Pass Listener");
69 | }
70 | }
71 | });
72 | }
73 |
74 | }
75 |
--------------------------------------------------------------------------------
/FragmentBackStack/src/com/chintanrathod/fragmentbackstack/MainActivity.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2014 The Android Open Source Project
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.chintanrathod.fragmentbackstack;
18 |
19 | import java.util.Stack;
20 |
21 | import android.os.Bundle;
22 | import android.support.v4.app.Fragment;
23 | import android.support.v4.app.FragmentActivity;
24 | import android.support.v4.app.FragmentManager;
25 | import android.support.v4.app.FragmentTransaction;
26 | import android.widget.Toast;
27 |
28 | import com.chintanrathod.fragmentbackstack.HomeListFragment.MyListFragmentListener;
29 |
30 | public class MainActivity extends FragmentActivity implements
31 | MyListFragmentListener {
32 |
33 | private Stack fragmentStack;
34 | private FragmentManager fragmentManager;
35 | private HomeListFragment homeListFragment;
36 | private ResultListFragment resultListFragment;
37 |
38 | @Override
39 | protected void onCreate(Bundle savedInstanceState) {
40 | super.onCreate(savedInstanceState);
41 | setContentView(R.layout.activity_main);
42 |
43 | fragmentStack = new Stack();
44 |
45 | homeListFragment = new HomeListFragment();
46 | homeListFragment.registerForListener(this);
47 |
48 | fragmentManager = getSupportFragmentManager();
49 | FragmentTransaction ft = fragmentManager.beginTransaction();
50 | ft.add(R.id.container, homeListFragment);
51 | fragmentStack.push(homeListFragment);
52 | ft.commit();
53 | }
54 |
55 | @Override
56 | public void onItemClickedListener(String valueClicked) {
57 | Toast.makeText(this, valueClicked, Toast.LENGTH_LONG).show();
58 |
59 | FragmentTransaction ft = fragmentManager.beginTransaction();
60 |
61 | resultListFragment = new ResultListFragment();
62 | ft.add(R.id.container, resultListFragment);
63 | fragmentStack.lastElement().onPause();
64 | ft.hide(fragmentStack.lastElement());
65 | fragmentStack.push(resultListFragment);
66 | ft.commit();
67 | }
68 |
69 | @Override
70 | public void onBackPressed() {
71 |
72 | if (fragmentStack.size() == 2) {
73 | FragmentTransaction ft = fragmentManager.beginTransaction();
74 | fragmentStack.lastElement().onPause();
75 | ft.remove(fragmentStack.pop());
76 | fragmentStack.lastElement().onResume();
77 | ft.show(fragmentStack.lastElement());
78 | ft.commit();
79 | } else {
80 | super.onBackPressed();
81 | }
82 | }
83 |
84 | }
85 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | Fragment-Back-Stack
2 | ===================
3 |
4 | Fragment Back Stack manager while displaying fragments on single activity and need to maintain on back press
5 |
6 | Purpose
7 | -------------
8 |
9 | We know that there is activity stack in Android. We don't need to maintain the stack while opening or closing
10 | activity. It will automatically handle the stack and show you the top of activity when you pressed `back` button.
11 |
12 | But in fragment, its neccessary to handle them. Because Android is not going to handle them. We need to create
13 | a stack of fragment and manage them while pressing `back` button.
14 |
15 | So, I have created one demo to represent how to handle the fragment in Back Stack.
16 |
17 | Usage
18 | -------------
19 |
20 | In the sample application, you will find one object named `fragmentStack`. Its a `Stack` which will
21 | push and pop the fragment as per requirement.
22 |
23 | Whenever you are displaying any new fragment, just push that fragment into stack using following code.
24 |
25 | //here this fragment is our first fragment
26 | homeListFragment = new HomeListFragment();
27 | fragmentStack.push(homeListFragment);
28 |
29 | 
30 |
31 | And when you are displaying any other fragment over this fragment, use following code.
32 |
33 | We will create a new object of second fragment and add it to stack.
34 |
35 | //here this fragment is second fragment
36 | resultListFragment = new ResultListFragment();
37 | //hide the last fragment
38 | ft.hide(fragmentStack.lastElement());
39 | //push the new fragment into stack
40 | fragmentStack.push(resultListFragment);
41 |
42 | 
43 |
44 | When `backPressed` event fires, we will check whether stack size is `2` or not. If it is, then we will pop last
45 | fragment and display the previous fragment by following code.
46 |
47 | if (fragmentStack.size() == 2) {
48 | FragmentTransaction ft = fragmentManager.beginTransaction();
49 | fragmentStack.lastElement().onPause();
50 | ft.remove(fragmentStack.pop());
51 | fragmentStack.lastElement().onResume();
52 | ft.show(fragmentStack.lastElement());
53 | ft.commit();
54 | } else {
55 | //if size is `1` it means first fragment is visible and we can exit from application
56 | super.onBackPressed();
57 | }
58 |
59 | MIT License
60 | -------------
61 |
62 | The MIT License (MIT)
63 |
64 | Copyright (c) 2015 Chintan Rathod
65 |
66 | Permission is hereby granted, free of charge, to any person obtaining a copy
67 | of this software and associated documentation files (the "Software"), to deal
68 | in the Software without restriction, including without limitation the rights
69 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
70 | copies of the Software, and to permit persons to whom the Software is
71 | furnished to do so, subject to the following conditions:
72 |
73 | The above copyright notice and this permission notice shall be included in all
74 | copies or substantial portions of the Software.
75 |
76 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
77 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
78 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
79 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
80 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
81 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
82 | SOFTWARE.
83 |
--------------------------------------------------------------------------------