导航回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

当您在清单中为Up Navigation指定父活动时,如下所示:

  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。