简体   繁体   中英

How to back without press “back” button

I'm writing a project to read pdf files. My main class uses this code to call the ReaderActivity class:

Intent it = new Intent(this, ReaderActivity.class);
startActivity(it);

And the ReaderActivity class is like below to read pdf files:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    String mimetype = "application/pdf";
    File file = new File(filepath);
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.fromFile(file), mimetype);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent);
}

When I successfully open the pdf, I press the "back" button and the view is black. When I press "back" again, it returns to the main class view. When the pdf opens, I want to press "back" button one time and return to the main class view. How can I do this?

don't save readActivity in history stack .

use

Intent it = new Intent(this, ReaderActivity.class);

it.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

You are calling ReaderActivity and from then you are calling intent to View pdf..

Now when you press back from pdf reading ..you come back to reader activity where you didn't have any layout set so you see black screen..

First Thing You should have directly called the view intent from your main activity.

BUT anyhow you created EXTRA activity for doing this..so you will have to remove that ReaderActivity as soon it is used so you can do that 2 ways..

1)

Intent it = new Intent(this, ReaderActivity.class); 
it.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(it); 

Or 2)

in onPause() of ReaderActivity Write this.finish();

Yes dear actually what happened, you are starting activity from you main class to open pdf and again your Reader class will start the activity for reading pdf. Thats why you are facing such problem.

For solving the problem dont open your Reader Activity from your main class. Try to open pdf from your main class . Because for reading pdf Intent is called and in that we used ACTION_VIEW so it is itself Activity.

Or another option is that finish your activity and call your main class on the BackPressed() Event. Or you can also setFlag to Intent. like, intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

I hope using this your problem will be solved.

You have to simply add a button in reader class. And on click listener of the button you have to simply finish the reader activity, as per stack mechanism it will automatically go to the previous activity if previous activity is not manually close.

this.finish ///in reader class on button click

The blank screen is coming because you are not setting any content view in ReaderActivity.So try to finish the ReaderActivity in onRestart() like this

this.finish();

Now when you press back button it will directly take you to the main activity.Hope this helps you.

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