Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  1. Implement two separate activities, and somehow allow swiping between them. The issues is that ViewPager can't be used with two separate activities.

  2. 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?

like image 335
Ralf Avatar asked Oct 29 '11 10:10

Ralf


1 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.

like image 64
Ralf Avatar answered Nov 01 '22 22:11

Ralf