简介: 我做了一个淡出/淡入淡出,淡出所有视图的不透明度。我想在切换活动之间制作动画。我现在必须将淡入/淡出添加到每个活动中的每个 onStop/onResume。
问题:是否可以创建一个自定义的重写 onStop 函数并在每个活动中使用它?
创建一个像这样的基础活动
public abstract class BaseActivity extends Activity {
..................
..................
@Override
public void onStop() {
super.onStop();
// Do your stuff
}
....................
...................
}
现在在每个活动中扩展基本活动,例如
public class Activity1 extends BaseActivity
This is a suggestion rather than the answer to your questions.
Based on your description, it seems like you want to define a custom transition animation between activities. Why not just define a Style? All you need to do is define windowExitAnimation
and windoEnterAnimation
in your custom style, and then override windowAnimationStyle
.
Check out the developer guide on how how to define your own style Styles and Themes.
是的,您绝对可以覆盖 onStop() 函数。使用 onStop 覆盖器创建活动 StopperActivity
@Override
public void onStop() {
super.onStop();
}
在其他活动中,只需扩展这个自定义活动而不是活动
创建类BaseActivity
例如
class BaseActivity extends Activity
{
void onResume()
{
super.onResume();
// your code here (Need to write only once for entire app)
}
void onStop()
{
super.onStop();
// your code here of onStop (Need to write only once for entire app)
}
}
现在将此BaseActivity用于您班级中的所有活动。
class MainActivity extends BaseActivity
{
...
}
class AnotherActivity extends BaseActivity
{
...
}
这样,您只需为 BaseActivity 中的淡入和淡出编写一次代码。