当我按下通知时如何打开标签片段?

时间:2016-04-12 10:54:56

标签: android android-fragments tabs android-notifications android-tablayout

我正在一个标签片段中尝试启动通知。我点击通知时需要打开特定标签。我试过这样的。

通知中的

     Intent intent = new Intent(context, NotificationTabs.class);
    // Send data to NotificationView Class
    intent.putExtra("Medicine", "Medicine");
    //intent.putExtra("text", "need to take medicine");
    // intent.setAction("Medicine");
    // Open NotificationView.java Activity
    PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);

并在主标签活动中

public class NotificationTabs extends AppCompatActivity {
TabLayout tabLayout;
ViewPager viewPager;
ViewPagerAdapter adapter;
ActionBar actionBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_notification_tabs);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
     actionBar = getSupportActionBar();
    tabLayout =(TabLayout) findViewById(R.id.tablayout);
    viewPager = (ViewPager)findViewById(R.id.viewpager);
    setupViewPager(viewPager);
    tabLayout.setupWithViewPager(viewPager);

    int position = 0;

    Intent intent = getIntent();

    String intentaction = intent.getAction();

    if (intentaction != null) {
        if (intentaction.equals("Medicine")) {
            position = 2;
        }
    }
     actionBar.setSelectedNavigationItem(position);//--- here i got error
}

private void setupViewPager(ViewPager viewPager) {
    adapter = new ViewPagerAdapter(getSupportFragmentManager());
    adapter.addFragment(new QuizFragment(),"Quiz");
    adapter.addFragment(new TipsFragment(),"Tips" );
    adapter.addFragment(new MedicineFragment(),"Medicine");

    viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public ViewPagerAdapter(FragmentManager manager) {

        super(manager);
    }
    public void addFragment(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);

    }

    @Override
    public Fragment getItem(int position) {

        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }


    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }
}

}

在我的代码中,任何人都可以帮助我在点击通知时如何打开片段?每当我按下通知时,我都需要在android中使用Medicine Fragment。在此先感谢。

3 个答案:

答案 0 :(得分:0)

使用

{{1}}
像这样

{{1}}

答案 1 :(得分:0)

传递操作指定选项卡以及未决意图中的意图。在您的活动检索操作(请参阅getIntent())传递并基于它,打开一个特定的选项卡。

Intent intent = new Intent(this, NotificationTabs.class);
intent.setAction("Medicine");
PendingIntent pending intent = PendingIntent.getService(this, 0, intent, 0);              



 public class NotificationTabs extends AppCompatActivity {
    TabLayout tabLayout;
    ViewPager viewPager;
    ViewPagerAdapter adapter;
    ActionBar actionBar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notification_tabs);
        //Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        //setSupportActionBar(toolbar);
        actionBar = getSupportActionBar();
        tabLayout =(TabLayout) findViewById(R.id.tablayout);
        viewPager = (ViewPager)findViewById(R.id.viewpager);
        setupViewPager(viewPager);
        tabLayout.setupWithViewPager(viewPager);







    }

    private void setupViewPager(ViewPager viewPager) {

        adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFragment(new QuizFragment(),"Quiz");
        adapter.addFragment(new TipsFragment(),"Tips" );
        adapter.addFragment(new MedicineFragment(),"Medicine");

        viewPager.setAdapter(adapter);
       if(getIntent().getAction().equals("Medicine") {

            actionBar.setSelectedNavigationItem(2);


        }else{
            actionBar.setSelectedNavigationItem(0);
        }

            adapter.notifyDataSetChanged();//here you have to notify      the adapter
    }
    class ViewPagerAdapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitleList = new ArrayList<>();

        public ViewPagerAdapter(FragmentManager manager) {

            super(manager);
        }
        public void addFragment(Fragment fragment, String title) {
            mFragmentList.add(fragment);
            mFragmentTitleList.add(title);

        }

        @Override
        public Fragment getItem(int position) {

            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }


        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitleList.get(position);
        }
    }
}

答案 2 :(得分:0)

样品

返回新的Intent(MyApplication.getContext(),DepositCLIA.class)。setAction(&#34; tab2deposit&#34;);

 <div class="flex-item w50 fill">
    <div class="flex-inner portfolio">
       <a href="">
          <img src="" alt="">
       </a>
    </div>
 </div>