【问题标题】:Restart animation of previous activity when onBackPressed()onBackPressed() 时重新启动上一个活动的动画
【发布时间】:2017-08-01 12:51:25
【问题描述】:

我有一个带有动画的GridView。当我们启动 activity animation 作品时,如果我们从那里转到另一个活动,我需要动画像启动时一样重新启动。现在当按下后退按钮时,它会转到上一个活动,但上述GridView 没有动画。代码如下:

CustomGrid adapter = new CustomGrid(Grids.this, web, imageId);
grid = (GridView) findViewById(R.id.grid);
grid.setAdapter(adapter);
Animation animation = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.grid_item_anim);
controller = new GridLayoutAnimationController(animation, .2f, .2f);
grid.setLayoutAnimation(controller);

【问题讨论】:

  • 在活动的onResume() 中执行此操作。

标签: android android-activity android-animation android-gridview


【解决方案1】:

通过此链接ActivityLifeCycle

这仅仅是因为我假设您在 onCreate(); 上编写了您的代码;当您启动活动时,它仅在您切换活动并返回时调用一次 onCreate(),您的状态将更改为 onResume(),因此您的动画代码停止工作。

您需要在onResume() 方法中编写您的代码,该方法在您启动活动时以及在您返回活动时调用。

当Activity第一次加载时,事件调用如下:

onCreate()
onStart()
onResume()

切换活动时

onPause()

返回活动时

onResume()

注意:需要注意的一件事是 onResume() 也会在配置更改发生时被调用。将代码放入 onResume() 将导致在您旋转设备时再次播放动画。 根据您的要求,您可能希望避免这种情况。

【讨论】:

    【解决方案2】:

    根据您的描述,我假设您将上面的代码放入活动的onCreate() 方法中。尝试将其放入onResume(),而不是onCreate()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-25
      • 2011-01-18
      • 1970-01-01
      • 2012-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多