完成活动而无需等待完成

时间:2013-09-04 11:53:44

标签: android android-activity activity-finish

通过找到完成活动的方法而不等待它完成,我感到有点折磨。 我知道一个简单的“回归”声明会做的伎俩,但在我的架构中我不能这样做。

我目前有:

public abstract class BaseFragmentActivity extends FragmentActivity {
     @Override
     protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          if (savedInstanceState != null) {
               finish();
               return;
          }
     }
}

public class MainActivity extends BaseFragmentActivity {
     @Override
     protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          /* code still executed here even when parent finish(); is called */
     }
}

一旦调用抽象类中的finish(),我怎样才能阻止在MainActivity中执行代码?

更新代码:

public abstract class BaseFragmentActivity extends FragmentActivity {
     @Override
     protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          if (savedInstanceState != null) {
               finish();
               return;
          }
     }
}

public class MainActivity extends BaseFragmentActivity {
     @Override
     protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          if (isFinishing()) {
              return;
          }
     }
}

奇怪的是,“返回”似乎有效,但我的FragmentManager以某种方式在最后启动了片段。

2 个答案:

答案 0 :(得分:1)

为什么不创建一个布尔值并将其设置为true并且将触发完成?

boolean end = false;

这里你做到了

public abstract class BaseFragmentActivity extends FragmentActivity {
     protected void onCreate(Bundle savedInstanceState) {
end = true;
     }
}

然后使用if语句

public class MainActivity extends BaseFragmentActivity {
     protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
if(end = false){
//stuff here}
else {
finish();
}
     }
}

答案 1 :(得分:1)

尝试将其包装在

if(isFinishing()){

    //code you don't want to execute if finish() is called

}