Scroll through Android actions like using ViewPager

In my Android application, I have two separate but related top-level screens / actions. I want them to have two separate launch icons, but allow the user to "sack" between them, as with ViewPager.

The options I see are:

  • Implement two separate actions and somehow allow them to scroll. The problems are that ViewPager cannot be used with two separate actions.

  • Implement one action with two fragments and use ViewPager to switch between them. Scrolling is easy, but is it possible to have two launchers that automatically switch to the correct fragment?

Is there any of the above two options, or is there something else I can try?

+6
source share
4 answers

I tried solution two, but the problem is that one action cannot determine which startup icon was used (please tell me if I'm wrong).

My solution was to add two “dummy” actions, which then trigger the main action, which matches the correct page number. The difficulty with this approach is to properly handle the task stack. When a trigger is selected, it is necessary to start an empty activity, and it should send the intention to the main action. Android is trying very hard to stop you from doing this, and just bring the latest activity to the fore.

This is the best I could come up with:

Fictitious actions (similar to 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 action:

 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 problem with different task similarities 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 - instead, just use the single launch icon.

+2
source

Solution 1: you can scroll in terms of triggering a different activity each time. In this case, you must fix the transition animation for two actions in order to have a “scroll” effect and set two actions as SingleTask so that there are no multiple instances in your task (thus, you must implement the onNewIntent () method).

Solution 2: Perhaps this is possible. Based on the launch icon, you should start and show the corresponding piece each time, and then use swipe to change views.

Hope this helps!

+1
source

I know this is old, but this is the first question I received when I was looking for the same problem.

You can find the solution there: ViewPager for several types of activities , use fragments ... (it is also in the support library if you need to run on earlier versions of Android)

0
source

I would go for solution 2, there is no need for fictitious actions, this will slow down the launch of your application, instead you can add activity aliases to your manifest, for example:

  <activity-alias android:name=".MySecondLauncher" android:exported="true" android:icon="@drawable/my_second_icon" android:label="@string/my_second_launcher" android:targetActivity=".MainScreen"> <meta-data android:name="secondLauncher" android:value="true" /> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity-alias> 

Then, in the MainScreen action, you can check with which account you were called, you can do this by looking for the name of the alias of the launch activity or by checking the above metadata:

 ComponentName component = intent.getComponent(); String name = component.getShortClassName(); boolean secondLauncher = ".MySecondLauncher".equals(name); 

or

 ActivityInfo info = getPackageManager().getActivityInfo(intent.getComponent(),PackageManager.GET_META_DATA); boolean secondLauncher = info.metaData.getBoolean("secondLauncher", false); 
0
source

Source: https://habr.com/ru/post/900335/


All Articles