Swipe between Android activities as with ViewPager
-
In my Android application I have two separate but related top-level screens / activities. I want to have two separate launcher icons for them, but allow the user to "swipe" between them, as with ViewPager. The options I see are: Implement two separate activities, and somehow allow swiping between them. The issues is that ViewPager can't be used with two separate activities. Implement a single activity with two fragments, and use the ViewPager to switch between them. The swiping is simple, but is it possible to have two launchers that automatically switch to the correct fragment? Is any of the above two options feasible, or is there something else I can try?
-
Answer:
Solution 1: You can swipe in terms of starting the other Activity each time. In this case you should fix the transition animation for the two activities in order to have a "swiping" effect and set the two activities as SingleTask in order not to have multiple instances in your task (thus you should implement the onNewIntent() method). Solution 2: Yeap this is possible. Based on the launcher icon, you should start and show the suitable fragment each time and then use swipe in order to change views. Hope this helps!
Ralf at Stack Overflow Visit the source
Other answers
I tried solution two, but the issue is that a single activity cannot detect which launcher icon was used (please tell me if I'm wrong). My solution was to add two "dummy" activities, which then launch the main activity which the correct "page" number. The difficulty with this approach is to handle the task stack properly. When the launcher is selected, the dummy activity must be launched, and it must send an intent to the main activity. Android tries really hard to prevent you from doing this, and just bring the last activity to the front again. This is the best I could come up with: The dummy activities (similar for LaunchActivity2): public class LaunchActivity1 extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Intent newIntent = new Intent(this, MainActivity.class); newIntent.putExtra(MainActivity.EXTRA_PAGE, 1); newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(newIntent); finish(); } } In the main activity: public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ViewPager viewPager = (ViewPager)findViewById(R.id.MainViewPager); viewPager.setAdapter(new MyAdapter(getSupportFragmentManager())); int page = getIntent().getIntExtra(EXTRA_PAGE, -1); if(page >= 0 && page <= NUM_ITEMS) viewPager.setCurrentItem(page); } public void onNewIntent(Intent intent) { if(intent.hasExtra(EXTRA_PAGE)) { int page = intent.getIntExtra(EXTRA_PAGE, -1); ViewPager viewPager = (ViewPager)findViewById(R.id.MainViewPager); if(page >= 0 && page <= NUM_ITEMS) viewPager.setCurrentItem(page); } } AndroidManifest.xml: <!-- LaunchActivity2 is similar --> <activity android:name=".LaunchActivity1" android:label="Launch 1" android:clearTaskOnLaunch="true" android:noHistory="true" android:taskAffinity="Launch1" android:theme="@android:style/Theme.Translucent.NoTitleBar"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> <activity android:name=".MainActivity" android:label="My App" android:clearTaskOnLaunch="true" android:finishOnTaskLaunch="true" android:launchMode="singleTask" android:taskAffinity="MyApp"> </activity> The issue with different task affinities is that both launchers, as well as the main task, appear in the "Recent Applications" list. I would not recommend this approach to anyone else - rather just use a single launcher icon.
Ralf
Related Q & A:
- How to use ViewPager?Best solution by Stack Overflow
- How to swap fragment in viewpager?Best solution by Stack Overflow
- Is it possible to swipe between images in Swift iOS?Best solution by Stack Overflow
- How to make an Android view that flips between views on swipe/fling?Best solution by Stack Overflow
- How you set up activities to help children learn and the most effective types of activities?Best solution by stayathomemoms.about.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.