简体   繁体   English

IntentExtras和选项卡式活动

[英]IntentExtras and tabbed activity

I used Android Studio new -> tabbed activity wizard to create, well, tabbed activity. 我使用Android Studio new->选项卡式活动向导来创建选项卡式活动。 I open that activity through intent from another activity's fragment. 我通过另一个活动的片段中的意图打开了该活动。 And I send additional data to this tabbed activity through intent extras. 然后,我通过意向附加功能将其他数据发送到此选项卡式活动。

I understand that I should catch intent extras in activity and then put that data to fragment arguments. 我知道我应该在活动中捕获意图额外的内容,然后将这些数据放入片段参数中。 I have done that multiple times in my app in regular blank activity with fragments. 我已经在我的应用程序中定期执行带有片段的空白活动多次。 But I seem to be lost where exactly in tabbed activity code should I catch intent extra and declare arguments to fragment. 但是我似乎迷路了,我应该在选项卡活动代码中的确切位置捕捉额外的意图并声明片段参数。 Can anybody offer any directions? 有人可以提供指示吗? As of now, I only got errors, regardless of where I put standard intent extra-> fragment argument code. 到目前为止,无论我将标准intent extra->片段参数代码放在何处,我都只会出错。

This is my tabbed activity code from Android studio template: 这是我来自Android Studio模板的选项卡式活动代码:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;

import java.util.Locale;


public class therapy_screen extends ActionBarActivity implements ActionBar.TabListener {

    /**
     * The {@link android.support.v4.view.PagerAdapter} that will provide
     * fragments for each of the sections. We use a
     * {@link FragmentPagerAdapter} derivative, which will keep every
     * loaded fragment in memory. If this becomes too memory intensive, it
     * may be best to switch to a
     * {@link android.support.v4.app.FragmentStatePagerAdapter}.
     */
    SectionsPagerAdapter mSectionsPagerAdapter;

    /**
     * The {@link ViewPager} that will host the section contents.
     */
    ViewPager mViewPager;

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

        // Set up the action bar.
        final ActionBar actionBar = getSupportActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // Create the adapter that will return a fragment for each of the three
        // primary sections of the activity.
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        // When swiping between different sections, select the corresponding
        // tab. We can also use ActionBar.Tab#select() to do this if we have
        // a reference to the Tab.
        mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                actionBar.setSelectedNavigationItem(position);
            }
        });

        // For each of the sections in the app, add a tab to the action bar.
        for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
            // Create a tab with text corresponding to the page title defined by
            // the adapter. Also specify this Activity object, which implements
            // the TabListener interface, as the callback (listener) for when
            // this tab is selected.
            actionBar.addTab(
                    actionBar.newTab()
                            .setText(mSectionsPagerAdapter.getPageTitle(i))
                            .setTabListener(this));
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.home_screen, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
        // When the given tab is selected, switch to the corresponding page in
        // the ViewPager.
        mViewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    }

    @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    }

    /**
     * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
     * one of the sections/tabs/pages.
     */
    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            // getItem is called to instantiate the fragment for the given page.
            return therapy_screen_fragment.newInstance(position + 1);
        }

        @Override
        public int getCount() {
            // Show 2 total pages.
            return 2;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            Locale l = Locale.getDefault();
            switch (position) {
                case 0:
                    return getString(R.string.title_section1).toUpperCase(l);
                case 1:
                    return getString(R.string.title_section2).toUpperCase(l);
            }
            return null;
        }
    }
}

I tried to catch and parse intent extra like: 我试图捕获并解析意图,例如:

@Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        return therapy_screen_fragment.newInstance(position + 1, getIntent().getExtras().getString("btnId"));
    }

But I get an error. 但是我得到一个错误。 I also tried to position getExtras() under "onCreate(Bundle savedInstanceState)" like I have in other blank activities. 我也尝试将getExtras()放在“ onCreate(Bundle savedInstanceState)”下,就像在其他空白活动中一样。 But I also get an error. 但是我也得到一个错误。

So, what is the right approach to catch intent extras in tabbed activity and then pars those arguments to fragment that display content under active tab? 那么,什么是正确的方法来捕获选项卡活动中的意图额外内容,然后解析这些参数以分段显示活动选项卡下的内容?

Oh, for f**k sake! 哦,为了缘故! I'm very sorry for taking your time! 很抱歉抽出宝贵的时间! Problem was that for this specific activity I had typo in my putExtra(). 问题在于,对于此特定活动,我在putExtra()中输入错误。 And I presumed that problem is in my method of getting those extras... And naturally, error was NullPointException (or something like that) and reason was that extra I was looking for was not set. 而且我认为问题出在我获取这些额外内容的方法上……自然,错误是NullPointException(或类似的东西),原因是我没有寻找额外的内容。 Thank you to NSimon for steering me in right direction with comment under question! 感谢NSimon引导我朝正确的方向提出疑问。

So, it is correct to get extras in onCreate method. 因此,在onCreate方法中获得额外功能是正确的。 Just to clarify if anybody gets in same dilemma. 只是为了澄清是否有人陷入同样的​​困境。

In end I used solution I already posted in answer (but was getting an error due to typo in putExtra): 最后,我使用了已经在答案中发布的解决方案(但由于putExtra中的拼写错误而出现错误):

@Override
public Fragment getItem(int position) {
    // getItem is called to instantiate the fragment for the given page.
    return therapy_screen_fragment.newInstance(position + 1, getIntent().getExtras().getString("btnId"));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM