12
12
import androidx .appcompat .widget .Toolbar ;
13
13
import androidx .fragment .app .Fragment ;
14
14
import androidx .fragment .app .FragmentManager ;
15
- import androidx .fragment .app .FragmentPagerAdapter ;
15
+ import androidx .fragment .app .FragmentStatePagerAdapter ;
16
16
import androidx .viewpager .widget .ViewPager ;
17
17
18
18
import com .google .android .material .floatingactionbutton .FloatingActionButton ;
21
21
import com .pleiades .pleione .base64 .ui .SettingsActivity ;
22
22
23
23
import org .apache .commons .codec .binary .Base64 ;
24
- import org .jetbrains .annotations .NotNull ;
24
+
25
+ import static androidx .fragment .app .FragmentStatePagerAdapter .BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT ;
25
26
26
27
public class MainActivity extends AppCompatActivity {
28
+ SectionsPagerAdapter sectionsPagerAdapter ;
27
29
ViewPager viewPager ;
28
30
29
31
String externalInput ;
@@ -38,21 +40,18 @@ protected void onCreate(Bundle savedInstanceState) {
38
40
Toolbar toolbar = appbar .findViewById (R .id .toolbar_main );
39
41
setSupportActionBar (toolbar );
40
42
41
- SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter (getSupportFragmentManager ());
43
+ sectionsPagerAdapter = new SectionsPagerAdapter (getSupportFragmentManager (), BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT );
42
44
viewPager = findViewById (R .id .pager );
43
45
viewPager .setAdapter (sectionsPagerAdapter );
44
46
TabLayout tabs = findViewById (R .id .layout_tab );
45
47
tabs .setupWithViewPager (viewPager );
46
48
FloatingActionButton fab = findViewById (R .id .fab );
47
49
48
- fab .setOnClickListener (new View .OnClickListener () {
49
- @ Override
50
- public void onClick (View view ) {
51
- int index = viewPager .getCurrentItem ();
52
- PlaceholderFragment fragment = (PlaceholderFragment ) sectionsPagerAdapter .instantiateItem (viewPager , index );
50
+ fab .setOnClickListener (view -> {
51
+ int index = viewPager .getCurrentItem ();
52
+ PlaceholderFragment fragment = (PlaceholderFragment ) sectionsPagerAdapter .instantiateItem (viewPager , index );
53
53
54
- fragment .convert ();
55
- }
54
+ fragment .convert ();
56
55
});
57
56
}
58
57
@@ -66,7 +65,8 @@ protected void onResume() {
66
65
if (intent .getAction ().equals (Intent .ACTION_PROCESS_TEXT ) && intent .hasExtra (Intent .EXTRA_PROCESS_TEXT )) {
67
66
externalInput = intent .getStringExtra (Intent .EXTRA_PROCESS_TEXT );
68
67
isExternalInputBase64 = Base64 .isBase64 (externalInput );
69
- viewPager .setCurrentItem (isExternalInputBase64 ? 1 : 0 );
68
+ int index = isExternalInputBase64 ? 1 : 0 ;
69
+ viewPager .setCurrentItem (index );
70
70
}
71
71
}
72
72
}
@@ -88,14 +88,13 @@ public boolean onOptionsItemSelected(@NonNull MenuItem item) {
88
88
return super .onOptionsItemSelected (item );
89
89
}
90
90
91
- class SectionsPagerAdapter extends FragmentPagerAdapter {
91
+ private class SectionsPagerAdapter extends FragmentStatePagerAdapter {
92
92
93
- // constructor
94
- public SectionsPagerAdapter (FragmentManager fm ) {
95
- super (fm , FragmentPagerAdapter .BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT );
93
+ public SectionsPagerAdapter (@ NonNull FragmentManager fm , int behavior ) {
94
+ super (fm , behavior );
96
95
}
97
96
98
- @ NotNull
97
+ @ NonNull
99
98
@ Override
100
99
public Fragment getItem (int position ) {
101
100
if (externalInput != null )
@@ -118,4 +117,35 @@ public int getCount() {
118
117
return 2 ;
119
118
}
120
119
}
120
+
121
+ // private class SectionsPagerAdapter extends FragmentPagerAdapter {
122
+ //
123
+ // // constructor
124
+ // public SectionsPagerAdapter(FragmentManager fm) {
125
+ // super(fm, FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
126
+ // }
127
+ //
128
+ // @NotNull
129
+ // @Override
130
+ // public Fragment getItem(int position) {
131
+ // if (externalInput != null)
132
+ // if ((position == 0 && !isExternalInputBase64) || position == 1 && isExternalInputBase64) {
133
+ // Fragment fragment = PlaceholderFragment.newInstance(position, externalInput);
134
+ // externalInput = null;
135
+ // return fragment;
136
+ // }
137
+ // return PlaceholderFragment.newInstance(position, null);
138
+ // }
139
+ //
140
+ // @Nullable
141
+ // @Override
142
+ // public CharSequence getPageTitle(int position) {
143
+ // return position == 0 ? "ENCODE" : "DECODE";
144
+ // }
145
+ //
146
+ // @Override
147
+ // public int getCount() {
148
+ // return 2;
149
+ // }
150
+ // }
121
151
}
0 commit comments