Is there a way to stop a runnable after only one execution of a method?
I have created this code to delay the starting of the method "getPreferences" but when I run the app my next activity that "getPreferences" sends me to gives this problem of the screen refreshing?
Is this down to the runnable still continuing to loop and how can I kill it after one execution?
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.Menu;
public class StartScreen extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start_screen);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
getPreferences();
}}, 10000);
}
private void getPreferences() {
// TODO Auto-generated method stub
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
String name = sharedPreferences.getString("NAME", "");
if (name != null) {
// the key does not exist
Intent intent=new Intent(StartScreen.this,InitialPreferences.class);
startActivity(intent);
}
if (name == "NAME"){
// handle the value
Intent intent=new Intent(StartScreen.this,MainActivity.class);
startActivity(intent);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.start_screen, menu);
return true;
}
}
InitialPreferences avtivity....
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
public class InitialPreferences extends StartScreen implements OnClickListener {
EditText editText1;
Button button1;
TextView textView1;
RadioGroup radioGroup;
RadioButton radioPosistionButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.initial_preferences);
// waitTimer.cancel();
textView1 = (TextView)findViewById(R.id.textView1);
editText1 = (EditText)findViewById(R.id.editText1);
button1 = (Button)findViewById(R.id.button1);
radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
}
@Override
protected void onStart(){
//waitTimer.cancel();
LoadPreferences();
super.onStart();
}
private void SavePreferences(String key, String value){
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(key, value);
editor.commit();
}
private void LoadPreferences(){
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
String name = sharedPreferences.getString("NAME", "");
textView1.setText(name);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
SavePreferences("NAME", editText1.getText().toString());
// get selected radio button from radioGroup
int selectedId = radioGroup.getCheckedRadioButtonId();
// find the radio button by returned id
radioPosistionButton = (RadioButton) findViewById(selectedId);
}
}