I'm programming a tiny application. Just for your background knowledge: If you click on the image, the counter decreases. Attending to that my 2 questions are the following.
- How can I manage it with some code of lines that my counter doesen't fall back to the startpoint when I leave the app. E.g. if I leave at point 5 and come back later, I want to continue at point 5 and not 10.
- I want that a completelly new picture appears when the counter reaches 0 but I do not know how to manage that.
To point 2: I have tried with the following lines but it doesn't work. The app crashes when I leave, e.g. at point 6.
@Override
protected void onPause() {
SharedPreferences mPrefs = getSharedPreferences("data", 0);
counter = mPrefs.getInt("counter", 0);
SharedPreferences.Editor
mEditor = mPrefs.edit();
mEditor.putInt("counter", counter).commit();
onWindowFocusChanged(true);
}
Here is the whole code. I hope someone is able to help me. :)
package com.first.app;
import android.app.Activity;
import android.app.Fragment;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends Activity {
int counter = 10;
public static String TAG = "Debug";
@Override
protected void onPause() {
SharedPreferences mPrefs = getSharedPreferences("data", 0);
SharedPreferences.Editor
mEditor = mPrefs.edit();
mEditor.putInt("counter", counter).commit();
onWindowFocusChanged(true);
super.onPause();
}
@Override
protected void onStop() {
Log.w(TAG, "App stopped");
//also do your savings here to be sure
super.onStop();
}
@Override
protected void onDestroy() {
Log.w(TAG, "App destoryed");
//also do your savings here to be sure
super.onDestroy();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView text = (TextView) findViewById(R.id.TextView01);
final ImageView apfel = (ImageView) findViewById(R.id.appel);
final ImageView image = (ImageView) findViewById(R.id.smiley);
apfel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (counter > 0) {
counter--;
text.setText("" + counter);
}
if (counter == 0) {
image.setImageResource(R.drawable.smiley);
text.setText("You finished!");
}
}
});
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
The things after the following codeline were automatically'installed' when I created the project.
if (counter == 0) {
text.setText("Congratulations you finished!");