如何从通知中打开片段?

时间:2021-05-21 21:44:48

标签: java android android-fragments notifications fragment

我有一个应用程序可以在按下“+”时创建一个新片段。每个片段都有当前片段的“通知创建按钮”。

  1. 点击后,我需要在当前片段上显示提醒通知。

  2. 我需要从通知(从任务栏)打开片段,例如:我从第 5 个片段创建通知,然后我将单击该通知,该操作必须打开第 5 个片段。我该如何解决?谢谢!

我的主要活动

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();
    }
}

我的初始片段

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());
}

0 个答案:

没有答案