Skip to content

Commit 2198451

Browse files
committed
Fragmentbackstack
Fragmentbackstack
1 parent 3fdf98f commit 2198451

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1289
-0
lines changed

FragmentBackProcessSample/.gitignore

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea/workspace.xml
5+
/.idea/libraries
6+
.DS_Store
7+
/build
8+
/captures

FragmentBackProcessSample/.idea/.name

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

FragmentBackProcessSample/.idea/compiler.xml

+22
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

FragmentBackProcessSample/.idea/copyright/profiles_settings.xml

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

FragmentBackProcessSample/.idea/encodings.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

FragmentBackProcessSample/.idea/gradle.xml

+23
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

FragmentBackProcessSample/.idea/misc.xml

+46
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

FragmentBackProcessSample/.idea/modules.xml

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

FragmentBackProcessSample/.idea/runConfigurations.xml

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
apply plugin: 'com.android.application'
2+
3+
android {
4+
compileSdkVersion 23
5+
buildToolsVersion "23.0.1"
6+
7+
defaultConfig {
8+
applicationId "com.samset.user.fragmentbackprocesssample"
9+
minSdkVersion 15
10+
targetSdkVersion 23
11+
versionCode 1
12+
versionName "1.0"
13+
}
14+
buildTypes {
15+
release {
16+
minifyEnabled false
17+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18+
}
19+
}
20+
}
21+
22+
dependencies {
23+
compile fileTree(dir: 'libs', include: ['*.jar'])
24+
testCompile 'junit:junit:4.12'
25+
compile 'com.android.support:appcompat-v7:23.4.0'
26+
compile 'com.android.support:support-v4:23.4.0'
27+
compile 'com.android.support:design:22.2.0'
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Add project specific ProGuard rules here.
2+
# By default, the flags in this file are appended to flags specified
3+
# in C:\Users\user\AppData\Local\Android\Sdk/tools/proguard/proguard-android.txt
4+
# You can edit the include path and order by changing the proguardFiles
5+
# directive in build.gradle.
6+
#
7+
# For more details, see
8+
# http://developer.android.com/guide/developing/tools/proguard.html
9+
10+
# Add any project specific keep options here:
11+
12+
# If your project uses WebView with JS, uncomment the following
13+
# and specify the fully qualified class name to the JavaScript interface
14+
# class:
15+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16+
# public *;
17+
#}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.samset.user.fragmentbackprocesssample;
2+
3+
import android.app.Application;
4+
import android.test.ApplicationTestCase;
5+
6+
/**
7+
* <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
8+
*/
9+
public class ApplicationTest extends ApplicationTestCase<Application> {
10+
public ApplicationTest() {
11+
super(Application.class);
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.samset.user.fragmentbackprocesssample">
4+
5+
<application
6+
android:allowBackup="true"
7+
android:icon="@mipmap/ic_launcher"
8+
android:label="@string/app_name"
9+
android:supportsRtl="true"
10+
android:theme="@style/AppTheme">
11+
<activity android:name=".MainActivity">
12+
<intent-filter>
13+
<action android:name="android.intent.action.MAIN" />
14+
15+
<category android:name="android.intent.category.LAUNCHER" />
16+
</intent-filter>
17+
</activity>
18+
</application>
19+
20+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.samset.user.fragmentbackprocesssample;
2+
3+
import android.support.v4.app.Fragment;
4+
import android.support.v4.app.FragmentManager;
5+
import android.support.v4.app.FragmentTransaction;
6+
import android.support.v7.app.AppCompatActivity;
7+
import android.os.Bundle;
8+
import android.util.Log;
9+
10+
import com.samset.user.fragmentbackprocesssample.fragments.HomeFragment;
11+
import com.samset.user.fragmentbackprocesssample.fragments.LoginFragment;
12+
13+
public class MainActivity extends AppCompatActivity {
14+
String TAG = MainActivity.class.getSimpleName();
15+
16+
@Override
17+
protected void onCreate(Bundle savedInstanceState) {
18+
super.onCreate(savedInstanceState);
19+
setContentView(R.layout.activity_main);
20+
fragTransaction(new LoginFragment());
21+
}
22+
23+
@Override
24+
protected void onResume() {
25+
super.onResume();
26+
27+
}
28+
29+
public void fragTransaction(Fragment frag) {
30+
if (frag.isAdded()) {
31+
Log.e(TAG, " fragment add");
32+
FragmentManager manager = getSupportFragmentManager();
33+
FragmentTransaction transaction = manager.beginTransaction();
34+
transaction.add(R.id.mainContainer, frag).addToBackStack(null).commit();
35+
} else {
36+
Log.e(TAG, " fragment replace");
37+
FragmentManager manager = getSupportFragmentManager();
38+
FragmentTransaction transaction = manager.beginTransaction();
39+
transaction.replace(R.id.mainContainer, frag).addToBackStack(null).commit();
40+
}
41+
}
42+
43+
44+
@Override
45+
public void onBackPressed() {
46+
47+
// here you simply back handle you check stack count and then enable backpress button
48+
if (getSupportFragmentManager().getBackStackEntryCount() > 1) {
49+
getSupportFragmentManager().popBackStack();
50+
Log.e(TAG, " fragment popback ");
51+
} else {
52+
super.onBackPressed();
53+
}
54+
}
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.samset.user.fragmentbackprocesssample.fragments;
2+
3+
4+
import android.os.Bundle;
5+
import android.support.annotation.Nullable;
6+
import android.support.v4.app.Fragment;
7+
import android.support.v4.app.FragmentManager;
8+
import android.support.v4.app.FragmentTransaction;
9+
import android.support.v7.widget.AppCompatButton;
10+
import android.view.LayoutInflater;
11+
import android.view.View;
12+
import android.view.ViewGroup;
13+
14+
import com.samset.user.fragmentbackprocesssample.R;
15+
16+
/**
17+
* A simple {@link Fragment} subclass.
18+
*/
19+
public class DetailsFragment extends Fragment {
20+
21+
AppCompatButton compatButton,btnHome;
22+
public DetailsFragment() {
23+
// Required empty public constructor
24+
}
25+
26+
27+
@Override
28+
public View onCreateView(LayoutInflater inflater, ViewGroup container,
29+
Bundle savedInstanceState) {
30+
View view =inflater.inflate(R.layout.fragment_details, container, false);
31+
32+
compatButton= (AppCompatButton) view.findViewById(R.id.btnlast);
33+
btnHome= (AppCompatButton) view.findViewById(R.id.btnanother);
34+
35+
36+
return view;
37+
}
38+
39+
@Override
40+
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
41+
super.onActivityCreated(savedInstanceState);
42+
43+
compatButton.setOnClickListener(new View.OnClickListener() {
44+
@Override
45+
public void onClick(View v) {
46+
47+
// popBackStack pops till the transaction with mentioned ‘tag’ if flag is POP_BACK_STACK_INCLUSIVE, or else 0 can be used.
48+
getFragmentManager().popBackStack("profile",FragmentManager.POP_BACK_STACK_INCLUSIVE);
49+
50+
// popBackStackImmediate mathod work same as a popBackStack
51+
//getFragmentManager().popBackStackImmediate("profile",FragmentManager.POP_BACK_STACK_INCLUSIVE);
52+
53+
}
54+
});
55+
56+
btnHome.setOnClickListener(new View.OnClickListener() {
57+
@Override
58+
public void onClick(View v) {
59+
60+
// popBackStack pops till the transaction with mentioned ‘tag’ if flag is POP_BACK_STACK_INCLUSIVE, or else 0 can be used.
61+
getFragmentManager().popBackStack("home",FragmentManager.POP_BACK_STACK_INCLUSIVE);
62+
63+
// popBackStackImmediate mathod work same as a popBackStack
64+
//getFragmentManager().popBackStackImmediate("profile",FragmentManager.POP_BACK_STACK_INCLUSIVE);
65+
66+
}
67+
});
68+
}
69+
}

0 commit comments

Comments
 (0)