I have code for a timer , but if i change fragment the timer was reset to 00:00 i want that timer still count after i click stop or pause or that mean this timer still count in the background how to do it ? this is my code,
public class TimerFragment extends BaseFragment {
private Button startButton;
private Button pauseButton;
private TextView timerValue;
private long startTime = 0L;
private Handler customHandler = new Handler();
long timeInMilliseconds = 0L;
long timeSwapBuff = 0L;
long updatedTime = 0L;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_timer, container, false);
initialize();
//when start
startTime = SystemClock.uptimeMillis();
customHandler.postDelayed(updateTimerThread, 0);
//pause
pauseButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
timeSwapBuff += timeInMilliseconds;
customHandler.removeCallbacks(updateTimerThread);
}
});
return rootView;
}
private Runnable updateTimerThread = new Runnable() {
public void run() {
timeInMilliseconds = SystemClock.uptimeMillis() - startTime;
updatedTime = timeSwapBuff + timeInMilliseconds;
int secs = (int) (updatedTime / 1000);
int mins = secs / 60;
secs = secs % 60;
timerValue.setText("" + mins + ":"
+ String.format("%02d", secs));
customHandler.postDelayed(this, 0);
}
};
thank you anyway
Problem :- When you switch fragment and come back to Timer Fragment, the onCreate lifecycle callback will be called and that gone reset your timer in
customHandler.postDelayed(updateTimerThread, 0);
Solution :- Move timer logic to Activity class but activity will be recreate in orientation change so move timer logic into Application class :-
Drop this class in you project package
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.Application;
import android.os.Handler;
import android.widget.Toast;
public class App extends Application {
public static App appInstance;
private SimpleDateFormat dateFormat;
@Override
public void onCreate() {
super.onCreate();
appInstance = this;
dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
}
public void afficher() {
Toast.makeText(getBaseContext(), dateFormat.format(new Date()), 300).show();
handler.postDelayed(runnable,1000);
}
public void startTimer() {
runnable.run();
}
public void stopTimer() {
handler.removeCallbacks(runnable);
}
Handler handler = new Handler();
Runnable runnable = new Runnable() {
public void run() {
afficher();
}
};
}
Set your Application name to App class like this below (android name property of application) :-
<application
android:name="com.masterdetail_webview.App"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.masterdetail_webview.TimertestActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Now you can start and stop timer simply like this from anywhere
findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
App.appInstance.startTimer();
}
});
findViewById(R.id.button2).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
App.appInstance.stopTimer();
}
});
Adding AppController
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments