导航回到后台活动的Andr​​oid [英] Navigate back to a background activity Android

查看:183
本文介绍了导航回到后台活动的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3项活动:
1.类别列表;
2.这一类的新闻;
3.关于新闻详情

I have 3 activities: 1. The list of categories; 2. The news in this category; 3. Details about News

看下面的图片!

我实现后退按钮从第三个活动到第二个通过设置第二个活动是在Android清单文件中的第三个活动的父活动,但不能看到,被选择的第一个活动的范畴,因此没有按'T显示列表和崩溃,任何人可以帮助我实现后退按钮?

I implemented back button from the third activity to the second by setting the second activity as the parent activity for the third activity in the android manifest file, but it can not see the category that is chosen in the first activity so it doesn't display the list and crash, can anybody help me implementing back button?

下面是活动的code,我想实现后退按钮

here is the code of activity where i want to implement the back button

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

推荐答案

当您在清单中指定的父活动向上导航是这样的:

When you specify parent activity in manifest for Up Navigation like this:

    android:parentActivityName

当你在操作栏中单击向上按钮,父活动被重新启动(在你的情况下,它是类的活动),来避免活动得到重启。只是将这一code在第三个活动(即详细描述有关新闻)完成在顶。

and when you click on Up button in action bar, the parent activity gets restarted (in your case it is category activity), instead to avoid activity get restarted. just finish the top activity by putting this code in your 3rd activity (i.e Details about News).

@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。

and remove the parentActivityName from manifest.

这篇关于导航回到后台活动的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆