簡體   English   中英

導航回后台活動 Android

[英]Navigate back to a background activity Android

我有 3 個活動:1. 類別列表; 2、本分類新聞; 3. 新聞詳情

看下面的圖片!

我通過在android清單文件中將第二個活動設置為第三個活動的父活動來實現從第三個活動到第二個活動的后退按鈕,但它看不到在第一個活動中選擇的類別,因此它不顯示列表和崩潰,有人能幫我實現后退按鈕嗎?

這是我想要實現后退按鈕的活動代碼

public class Categorii_LIst_Item_Clicked extends ActionBarActivity {

static Context context;
static Bundle extras;

SectionsPagerAdapter mSectionsPagerAdapter;
static ImageLoader imageLoader;
static DisplayImageOptions options;

ViewPager mViewPager;

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

        context = this;
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        extras = getIntent().getExtras();

        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        //Setup the ImageLoader, we'll use this to display our images
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this).build();
        imageLoader = ImageLoader.getInstance();
        imageLoader.init(config);

        //Setup options for ImageLoader so it will handle caching for us.
        options = new DisplayImageOptions.Builder()
                .cacheInMemory()
                .cacheOnDisc()
                .build();


    }



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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

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

    return super.onOptionsItemSelected(item);
}


public class SectionsPagerAdapter extends FragmentPagerAdapter {

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

@Override
public Fragment getItem(int position) {
    if (position == 0) {
        return PlaceholderFragment.newInstance(position);
    } else if (position == 1) {
        return VideoFragment.newInstance1(position);
    }
    return PlaceholderFragment.newInstance(position);
}

@Override
public int getCount() {
    return 2;
}

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


public static class PlaceholderFragment extends Fragment {


private static final String ARG_SECTION_NUMBER = "section_number";


public static PlaceholderFragment newInstance(int sectionNumber) {
    PlaceholderFragment fragment = new PlaceholderFragment();
    Bundle args = new Bundle();
    args.putInt(ARG_SECTION_NUMBER, sectionNumber);
    fragment.setArguments(args);
    return fragment;
}

public PlaceholderFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_list_item_clicked, container, false);


    TextView pDate = (TextView) rootView.findViewById(R.id.textView);
    pDate.setText(extras.getString("pdate"));


    TextView ptitle = (TextView) rootView.findViewById(R.id.section_label);
    ptitle.setText(extras.getString("pname"));


    TextView pnText = (TextView) rootView.findViewById(R.id.textView2);
    pnText.setText(extras.getString("pText"));


    //Setup a listener we can use to swtich from the loading indicator to the Image once it's ready
    ImageLoadingListener listener = new ImageLoadingListener() {


        @Override
        public void onLoadingStarted(String arg0, View arg1) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onLoadingCancelled(String arg0, View arg1) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onLoadingComplete(String arg0, View arg1, Bitmap arg2) {
            // i/ndicator.setVisibility(View.INVISIBLE);
            // iconImg.setVisibility(View.VISIBLE);
        }

        @Override
        public void onLoadingFailed(String arg0, View arg1, FailReason arg2) {
            // TODO Auto-generated method stub

        }

    };

    //Load the image and use our options so caching is handled.
    final ImageView iconImg = (ImageView) rootView.findViewById(R.id.imageView);
    imageLoader.displayImage(extras.getString("pImage"), iconImg, options, listener);


    return rootView;
}
}


public static class VideoFragment extends Fragment {


private static final String ARG_SECTION_NUMBER = "section_number";


public static VideoFragment newInstance1(int sectionNumber) {
    VideoFragment fragment = new VideoFragment();
    Bundle args = new Bundle();
    args.putInt(ARG_SECTION_NUMBER, sectionNumber);
    fragment.setArguments(args);
    return fragment;
}

public VideoFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.video_fragment, container, false);
    VideoView myVideoView = (VideoView) rootView.findViewById(R.id.videoView);
    Uri video = Uri.parse(extras.getString("pVideo"));
    myVideoView.setMediaController(new MediaController(context));
    myVideoView.setVideoURI(video);
    myVideoView.requestFocus();
    //myVideoView.start();

    return rootView;
}
}
}

1

2

3

當您在清單中為向上導航指定父活動時:

    android:parentActivityName

並且當您單擊操作欄中的向上按鈕時,父活動將重新啟動(在您的情況下它是類別活動),而不是避免活動重新啟動。 只需將此代碼放在您的第 3 個活動中即可完成最上面的活動(即有關新聞的詳細信息)。

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    // Respond to the action bar's Up/Home button
    case android.R.id.home:
        finish();
        return true;
    }
    return super.onOptionsItemSelected(item);
}

並從清單中刪除 parentActivityName。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM