简体   繁体   中英

Navigate back to a background activity Android

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

see the images below!

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?

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

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 (ie 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);
}

and remove the parentActivityName from manifest.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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