Activity transition does not work as expected with background color
There are two operations in my application (A and B). Both actions have a background color. When I open Activity B from Activity A using intent, the background color changes to gray by default for a moment. This default color burst makes it so the transition doesn't look smooth. How can I make it smooth?
Action A XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#116493" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
Action B XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#116493">
</LinearLayout>
A.java actions
package com.example.test;
import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView tt1;
private Handler customHandler = new Handler(Looper.getMainLooper());
long timeInMilliseconds = 0L,timeToGo=0L,startTime=0L;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tt1=(TextView) findViewById(R.id.textView1);
startTime=System.currentTimeMillis();
customHandler.postDelayed(updateTimerThread, 0);
}
public Runnable updateTimerThread=new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
long timeNow = System.currentTimeMillis();
timeToGo = 30 - (timeNow - startTime) / 1000;
tt1=(TextView) findViewById(R.id.textView1);
tt1.setText(timeToGo+"");
if(timeToGo<0L){
Intent intent=new Intent(MainActivity.this,Game.class);
finish();
startActivity(intent);
}
else
customHandler.postDelayed(this, 0);
}
};
}
Action B..java
package com.example.test;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class Game extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
}
}
source to share
I found a solution. In Activity A, I used finish () before starting another intent. Thus, the previous activity is closed by the first and the next. There is a short time lag between these two actions and it is treated as a standard gray color flash. This is because there is no activity during this short time and the default color is flashing.
I solved the problem by starting over and then finishing the previous operation using finish.
Action code A
package com.example.test;
import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView tt1;
private Handler customHandler = new Handler(Looper.getMainLooper());
long timeInMilliseconds = 0L,timeToGo=0L,startTime=0L;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tt1=(TextView) findViewById(R.id.textView1);
startTime=System.currentTimeMillis();
customHandler.postDelayed(updateTimerThread, 0);
}
public Runnable updateTimerThread=new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
long timeNow = System.currentTimeMillis();
timeToGo = 30 - (timeNow - startTime) / 1000;
tt1=(TextView) findViewById(R.id.textView1);
tt1.setText(timeToGo+"");
if(timeToGo<0L){
Intent intent=new Intent(MainActivity.this,Game.class);
startActivity(intent); //Exchanged the order of statement
finish(); //After starting intent
}
else
customHandler.postDelayed(this, 0);
}
};
}
source to share
You are doing too much computation on the UI thread, so it lags behind. Especially findViewById
- a very difficult operation. Try this instead:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tt1=(TextView) findViewById(R.id.textView1);
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
tt1.setText(millisUntilFinished / 1000 + "");
}
public void onFinish() {
// tt1.setText("done!");
Intent intent=new Intent(MainActivity.this,Game.class);
finish();
startActivity(intent);
}
}.start();
}
source to share