关闭其他活动中的活动?

发布于 2024-11-28 01:14:52 字数 447 浏览 1 评论 0原文

有谁知道如何从其他活动中关闭一个活动?例如:我有 3 个活动(活动 A、B 和 C),从活动 C 中,我可以关闭活动 A.. 我的活动结构是活动A ->活动B->活动C 如何从活动C中关闭活动A?

我尝试了这个代码:

@Override

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Intent goToLockScreen= new Intent(this,LockScreenForm.class);
        startActivity(goToLockScreen);
        finish();

但该代码只能从活动 B 中关闭活动 A,并且不能直接从活动 C 中关闭活动 A。

有谁知道如何直接从其他活动中关闭活动? 谢谢..

Does anyone know how to close an activity from other activity?? for example: i have 3 activity (activity A, B, and C) and from activity C, i can close an activity A..
my activity structure is activity A -> activity B -> activity C
how to close an activity A from activity C?

i was try this code :

@Override

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Intent goToLockScreen= new Intent(this,LockScreenForm.class);
        startActivity(goToLockScreen);
        finish();

but that code is only fot closing activity A from activity B, and can't close activity A from activity C direcly..

Does anyone know about closing an activity direcly from other activity??
thanks..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

顾冷 2024-12-05 01:14:52
First Go to parent activity by starting it
@Override
 protected void onActivityResult(int requestCode, int resultCode, Intent intent)
{
super.onActivityResult(requestCode, resultCode, intent);
switch(Code){
 case A: go to that activity and finsih() this again come back to parent activity
 case B: go to that activity and finsih() this again come back to parent activity
/////and son on
}
First Go to parent activity by starting it
@Override
 protected void onActivityResult(int requestCode, int resultCode, Intent intent)
{
super.onActivityResult(requestCode, resultCode, intent);
switch(Code){
 case A: go to that activity and finsih() this again come back to parent activity
 case B: go to that activity and finsih() this again come back to parent activity
/////and son on
}
美煞众生 2024-12-05 01:14:52

尝试它对我来说完美地工作

   `public class aActivity extends Activity {

    public static Activity handleToClose;

    @Override
          public void onCreate(Bundle savedInstanceState) {
    .
    .
    .
    handleToClose = this;
    }

    public void onClick(View v)
    {
   Intent i = new Intent(this, act2.class);
   startActivity(i);
     }
    }`

现在你必须完成活动B中的活动A

活动B或第二个活动

    `public class act2 extends Activity {
 public void onCreate(Bundle savedInstanceState) {
      // your code here
          }

     public void onClick(View v)
     {
    aActivity.handleToClose.finish(); //this will finish aActivity (1st Activity)
     finish();//to finish current Activity
     }
      }`

try It work perfectly for me

   `public class aActivity extends Activity {

    public static Activity handleToClose;

    @Override
          public void onCreate(Bundle savedInstanceState) {
    .
    .
    .
    handleToClose = this;
    }

    public void onClick(View v)
    {
   Intent i = new Intent(this, act2.class);
   startActivity(i);
     }
    }`

Now You have to finish Activity-A from Activity-B

Activity-B or 2nd Activity

    `public class act2 extends Activity {
 public void onCreate(Bundle savedInstanceState) {
      // your code here
          }

     public void onClick(View v)
     {
    aActivity.handleToClose.finish(); //this will finish aActivity (1st Activity)
     finish();//to finish current Activity
     }
      }`
十雾 2024-12-05 01:14:52
Intent goToLockScreen= new Intent(this,LockScreenForm.class);
goToLockScreen.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

这是规定的方式,你应该遵循它..如果你想要一些其他行为,你可以实现它..关于这个主题有很多问题被问到..参考其他问题...

Intent goToLockScreen= new Intent(this,LockScreenForm.class);
goToLockScreen.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

This is a prescribed way and you should follow it.. if you want some other behaviour yo can implement it.. there are lot many questions asked on this topic.. refer other questions...

我的痛♀有谁懂 2024-12-05 01:14:52

启动 B 和 C forResult 并将结果发送回之前的活动,让 A 最终调用 finish() 怎么样?像这样:

startActivityForResult() ->; B startActivityForResult()-> C
C setResult()-> B onActivityResult(){setResult()} -> C onActivityResult(){finish()}

听起来很复杂,但也许它可以用作解决方法?

What about starting both B and C forResult and send a result back to pervious activity to let A finally call finish()? Like this:

A startActivityForResult() -> B startActivityForResult()-> C
C setResult()-> B onActivityResult(){setResult()} -> C onActivityResult(){finish()}

Sounds complicated, but maybe it can be used as a workaround?

北恋 2024-12-05 01:14:52

尝试一下

如果更新活动正在启动另一个安装活动,那么您可能需要在更新活动中重写 void onActivityResult(int requestCode, int resultCode, Intent Intent) ,并提供以下实现。此外,当更新活动启动安装活动时,应该使用 startActivityForResult(Intent, int),而不是 startActivity(Intent)

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent)
{
super.onActivityResult(requestCode, resultCode, intent);
finish();
}

try this

If the update activity is launching another installation activity, then you may want to override void onActivityResult(int requestCode, int resultCode, Intent intent) in the update activity, providing the following implementation. Also, when the update activity launches the installation activity, it should do so with startActivityForResult(Intent, int), not with startActivity(Intent).

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent)
{
super.onActivityResult(requestCode, resultCode, intent);
finish();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文