Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
100 views
in Technique[技术] by (71.8m points)

Finish parent and current activity in Android

I have 3 activities. Activity A which leads to activity B, which in turn can go back to activity A or start activity C. However, if I press back in activity C the app should close.

To sum up:

  • Activity A starts activity B
  • Pressing Back on activity B should lead to A
  • Activity B starts activity C
  • Pressing Back on activity C should close the app

How should I go from activity B to C? This code currently gives me a NullPointerException on the last line:

Intent intent=new Intent(ActivityB.this, ActivityC.class);
startActivity(intent);
ActivityB.this.finish();
ActivityB.this.getParent().finish();

If I switch the last two lines I also get a null pointer.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I don't know if this will work, but you could try it:

  • From Activity A, start activity B for a result using startActivityForResult()

  • In Activity B, when the user triggers Activity C, start activity C.

startActivity() returns immediately, so

  • set a result that will inform A to finish as well,

  • Call finish() in B.

  • When A receives that result from B, A calls finish() on itself as well.

Failing that, you could make Activity C into its own app and then close the first app (with A & B) after it starts the second.

P.S. Take Falmarri's comment into consideration as you move forward!
Good luck.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...