How to open fragment from notification?

Viewed 213

I have an app that create a new fragment when pressed "+". Each fragment have "notification create button" for current fragment.

  1. After click I need show heads-up notification on current fragment.

  2. I need to open fragment from notification (from task-bar), for example: I create notification from 5th fragment and then i will click that notification, that action must open 5th fragment. How i can solve it? Thank you!

My MainActivity

public class MainActivity extends FragmentActivity {

public ViewPager2 viewPager;
public FragmentStateAdapter pagerAdapter;

public FragmentTransaction fragmentTransaction;
public InitFragment initFragment;
public String CHANNEL_ID = "1";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_layout);

    createNotificationChannel();


    initFragment = new InitFragment();

    fragmentTransaction = getSupportFragmentManager().beginTransaction();
    fragmentTransaction.add(R.id.fragment, initFragment);
    fragmentTransaction.commit();



    viewPager = findViewById(R.id.view_pager_2);
    pagerAdapter = new MainActivityAdapter(this);
    viewPager.setAdapter(pagerAdapter);

}

private void createNotificationChannel() {

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence name = getString(R.string.channel_name);
        String description = getString(R.string.channel_description);
        int importance = NotificationManager.IMPORTANCE_HIGH;

        NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
        channel.setDescription(description);

        NotificationManager notificationManager = getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }
}


@Override
public void onBackPressed() {
    if (viewPager.getCurrentItem() == 0) {
        super.onBackPressed();
    } else {
        viewPager.setCurrentItem(viewPager.getCurrentItem() - 1);
    }
}



private class MainActivityAdapter extends FragmentStateAdapter {
    public MainActivityAdapter(FragmentActivity fa) {
        super(fa);
    }

    @Override
    public Fragment createFragment(int position) {
        return new InitFragment();
    }

    @Override
    public int getItemCount() {
        return getSupportFragmentManager().getBackStackEntryCount();
    }
}

My InitFragment

public class InitFragment extends Fragment {

public FragmentTransaction fragmentTransaction;
public Integer notificationId, i;
public TextView tv;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}


@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);



    ImageButton ibCreate = view.findViewById(R.id.fragment_create_btn);
    ImageButton ibDelete = view.findViewById(R.id.fragment_delete_btn);
    Button btn = view.findViewById(R.id.notification_create_btn);


    tv = view.findViewById(R.id.fragment_counter_tv);

    if(getParentFragmentManager().getBackStackEntryCount() == 0){

        ibDelete.setVisibility(View.INVISIBLE);
        tv.setText("1");
    }


    View.OnClickListener ocl = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            fragmentTransaction = getParentFragmentManager().beginTransaction();
            fragmentTransaction.add(R.id.fragment, new InitFragment());
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();

        }

    };

    ibCreate.setOnClickListener(ocl);


    View.OnClickListener ocl2 = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            fragmentTransaction = getParentFragmentManager().beginTransaction();
            fragmentTransaction.remove(InitFragment.this);
            getParentFragmentManager().popBackStack();
            fragmentTransaction.commit();
        }
    };
    ibDelete.setOnClickListener(ocl2);


    View.OnClickListener ocl3 = new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            notificationInit();

        }
    };
    btn.setOnClickListener(ocl3);

    i = getParentFragmentManager().getBackStackEntryCount() + 1;
    tv.setText(i.toString());
    notificationId = Integer.parseInt(tv.getText().toString());


}
public void notificationInit(){

    String CHANNEL_ID = "1";

    Intent intent = new Intent(getContext(), InitFragment.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(getActivity(), 0,
            intent, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(getContext(), CHANNEL_ID)
            .setSmallIcon(R.drawable.ic_launcher_background)
            .setContentTitle(tv.getText())
            .setContentText(tv.getText())
            .setPriority(NotificationCompat.PRIORITY_MAX)
            .setContentIntent(pendingIntent);

    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(getActivity());
    notificationManager.notify(notificationId, builder.build());
}
1 Answers

I also spent a lot of time seeking for the solution for this problem

Intent intent = new Intent(getContext(), InitFragment.class);
PendingIntent pendingIntent = PendingIntent.getActivity(getActivity(), 0,
            intent, PendingIntent.FLAG_UPDATE_CURRENT);

You need to pass an action to intent using setAction(fragmentId). setAction() should take the position of your fragment in ViewPager. And then, in MainActivity you need to detect that action using intent.getAction() and navigate to your viewPager's page

intent.setAction("1") // fragment's position

In MainActivity:

val i = intent.getAction.toString()
Log.e(TAG, "Navigating to page number " + i)
viewPager.setCurrentItem(i.toInt())
Related