英文:
How to make animation only run when open or restart app in adnroid studio
问题
在应用程序的欢迎界面中,您放置了一些动画,将它们捆绑到一个名为animation()
的函数中,然后将该函数放在onCreate
方法中。问题是,当您从第二个活动转到第一个活动时,动画不会停止,即使onCreate
方法只调用一次,动画仍然不停地运行。所以我想要问的问题是,如何让animation()
仅在应用程序打开或重新启动时运行。
这是您提供的代码的翻译:
Activity 1
package com.example.stopanimation;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView view;
Button button;
boolean isFirstTime = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
view = findViewById(R.id.text);
button = findViewById(R.id.button);
if (isFirstTime) {
animation();
isFirstTime = false;
}
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, MainActivity2.class);
startActivity(intent);
}
});
}
public void animation() {
Animation animation = AnimationUtils.loadAnimation(this, R.anim.alpha);
Animation animatio = AnimationUtils.loadAnimation(this, R.anim.alpha);
view.startAnimation(animation);
button.startAnimation(animatio);
}
}
Activity 2
package com.example.stopanimation;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity2 extends AppCompatActivity {
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
button = findViewById(R.id.back);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
back();
}
});
}
@Override
public void onBackPressed() {
super.onBackPressed();
overridePendingTransition(0, 0);
}
public void back() {
// 执行返回之前必要的任务
finish(); // 关闭当前活动
overridePendingTransition(0, 0);
startActivity(new Intent(MainActivity2.this, MainActivity.class));
}
}
您提到尝试在onPause
和onResume
中添加overridePendingTransition(0, 0)
,但似乎没有解决问题。如果您有进一步的问题或需要其他帮助,请随时提出。
英文:
I have a big program for the end of year project, in which in the welcome interface of the application I put in a few animations, bundle it into an animation() function and put it in onCreate. The problem here is that it won't stop when I move from the 2nd Activity to the first with Intent, it keeps running non-stop even though the Activity lifecycle is onCreate called only once. So the question I want to ask is how can we make animation() only run when the app opens or restarts.
Here is the simple example of code that i just create to try fix this problem.
Activity 1
package com.example.stopanimation;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView view;
Button button;
boolean isFirstTime = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
view = findViewById(R.id.text);
button = findViewById(R.id.button);
if (isFirstTime) {
animation();
isFirstTime = false;
}
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, MainActivity2.class);
startActivity(intent);
}
});
}
public void animation(){
Animation animation = AnimationUtils.loadAnimation(this,R.anim.alpha);
Animation animatio = AnimationUtils.loadAnimation(this,R.anim.alpha);
view.startAnimation(animation);
button.startAnimation(animatio);
}
}
Activity 2
package com.example.stopanimation;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity2 extends AppCompatActivity {
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
button = findViewById(R.id.back);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
back();
}
});
}
@Override
public void onBackPressed() {
super.onBackPressed();
overridePendingTransition(0, 0);
}
public void back(){
// perform any necessary tasks before going back
finish(); // close the current activity
overridePendingTransition(0, 0);
startActivity(new Intent(MainActivity2.this,MainActivity.class));
}
}
I did try write overridePendingTransition(0, 0); on onPaused, onResume but i did'nt seem to
答案1
得分: 1
问题出在back()
函数的逻辑上,通过再次调用startActivity()
会导致第一个活动重新进入onCreate()
方法。解决方法是将其替换为finish()
,这样生命周期会直接进入onRestart()
和onResume()
方法。
public void back(){
// 在返回前执行必要的任务
finish(); // 关闭当前活动
}
英文:
The problem is in the logic of the back()
function, which by calling startActivity()
again causes the first activity to enter the onCreate()
method.
The solution would be to leave it alone in a finish()
so that the lifecycle enters the onRestart()
and onResume()
method directly.
public void back(){
// perform any necessary tasks before going back
finish(); // close the current activity
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论