I am creating a Pomodoro Timer app. It has 2 activities:
- Main activity is for setting a time.
- Recent activity contains recent times that were set.
I have created a Shared Preferences
object that saves the set times and I want these times to be displayed in the button in the Recents Activity. However, it seems as if the data is not being saved, instead the package name is being displayed.
Main Activity
package com.example.pomotimer;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
import java.util.Locale;
public class Activity2 extends AppCompatActivity {
public static final String EXTRA_TEXT = "com.example.pomotimer.EXTRA_TEXT";
private TextView TextViewCountdown;
private Button Button_Start_Pause;
private Button Reset;
SharedPreferences sp;
private Button buttonSet;
private Button select_Time;
private EditText edit_Text_Input;
private CountDownTimer countDownTimer;
public long mStartTimeinMillis;
private long timeLeft = mStartTimeinMillis;
private boolean timerRunning;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
TextViewCountdown = findViewById(R.id.textViewCount);
Button_Start_Pause = findViewById(R.id.bnStartPause);
Reset = findViewById(R.id.reset);
select_Time = findViewById(R.id.selectTime);
edit_Text_Input = findViewById(R.id.edit_Text_Input);
buttonSet = findViewById(R.id.buttonSet);
sp = getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
select_Time.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
edit_Text_Input.setVisibility(View.VISIBLE);
buttonSet.setVisibility(View.VISIBLE);
buttonSet.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String input = edit_Text_Input.getText().toString();
if (input.length() == 0){
Toast.makeText(Activity2.this, "Field can't be empty", Toast.LENGTH_SHORT).show();
return;
}
long millisInput = Long.parseLong(input)*60000;
if (millisInput == 0){
Toast.makeText(Activity2.this, "Timer cannot be set to 0", Toast.LENGTH_SHORT).show();
return;
}
setTime(millisInput);
edit_Text_Input.setText("");
}
});
}
});
Button_Start_Pause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (timerRunning){
pauseTimer();
}
else{
startTimer();
}
}
});
Reset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
resetTimer();
}
});
int minutes = (int) (timeLeft/1000)/60;
int seconds = (int) (timeLeft/1000)`;
String timeLeftFormatted = String.format(Locale.getDefault(),"d:d", minutes, seconds);
TextViewCountdown.setText(timeLeftFormatted);
}
@Override
public void finish() {
super.finish();
overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
}
public void setTime(long milliseconds){
mStartTimeinMillis = milliseconds;
resetTimer();
SP();
}
private void startTimer(){
countDownTimer = new CountDownTimer(timeLeft,1000) {
@Override
public void onTick(long millisUntilFinished) {
// The countdown timer has an automatic method of reducing time by 1s
timeLeft = millisUntilFinished;
int minutes = (int) (timeLeft/1000)/60;
int seconds = (int) (timeLeft/1000)`;
String timeLeftFormatted = String.format(Locale.getDefault(),"d:d", minutes, seconds);
TextViewCountdown.setText(timeLeftFormatted);
}
@Override
public void onFinish() {
timerRunning = false;
Button_Start_Pause.setText("Start");
Button_Start_Pause.setVisibility(View.INVISIBLE);
}
}.start();
timerRunning = true;
Button_Start_Pause.setText("pause");
}
int data = (int) timeLeft;
private void pauseTimer(){
countDownTimer.cancel();
timerRunning = false;
Button_Start_Pause.setText("Start");
}
private void resetTimer(){
timeLeft = mStartTimeinMillis;
int minutes = (int) (timeLeft/1000)/60;
int seconds = (int) (timeLeft/1000)`;
Button_Start_Pause.setVisibility(View.VISIBLE);
String timeLeftFormatted = String.format(Locale.getDefault(),"d:d", minutes, seconds);
TextViewCountdown.setText(timeLeftFormatted);
}
@Override
protected void onStop() {
super.onStop();
SharedPreferences prefs = getSharedPreferences("prefs", MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putLong("millisleft", timeLeft);
editor.putBoolean("timerRunning", timerRunning);
editor.apply();
}
@Override
protected void onStart() {
super.onStart();
SharedPreferences prefs = getSharedPreferences("prefs", MODE_PRIVATE);
timeLeft = prefs.getLong("millisleft",mStartTimeinMillis);
timerRunning = prefs.getBoolean("timeRunning", false);
int minutes = (int) (timeLeft/1000)/60;
int seconds = (int) (timeLeft/1000)`;
String timeLeftFormatted = String.format(Locale.getDefault(),"d:d", minutes, seconds);
TextViewCountdown.setText(timeLeftFormatted);
}
public void SP() {
String data = TextViewCountdown.toString();
SharedPreferences.Editor editor = sp.edit();
editor.putString("time", data);
editor.commit();
Toast.makeText(Activity2.this, "this ", Toast.LENGTH_SHORT).show();
}
}
Recents Activity
package com.example.pomotimer;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
public class recentActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recent);
Button button = findViewById(R.id.button_data);
// String number = getIntent().getStringExtra("keydata");
// button.setText(number);
SharedPreferences sp = getApplicationContext().getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
String time = sp.getString("time", "");
button.setText(time);
}
}
CodePudding user response:
Instead of String data = TextViewCountdown.toString();
it should be String data = TextViewCountdown.getText().toString();