Darko Drogriski Darko Drogriski - 6 months ago 22
Java Question

Android app save data

I recently started making android apps. The app I'm working on right now is a clicker game. I need the app to save the players money. Is there any way that i can do that without using mySQL?
Here's my MainActivity code.

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
int balance;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Hide notification bar
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
//Click counter
final TextView text = (TextView) findViewById(R.id.balance_text);
assert text != null;
text.setText("0");
final ImageButton button = (ImageButton) findViewById(R.id.click_button);
assert button != null;
button.setOnClickListener(new View.OnClickListener(){

@Override
public void onClick(View v){
balance++;
text.setText("" + balance);
}
});
}
}

Answer

There's a great article called Storage Options, take a look and pick an approach that fits your use case best. Probably SharedPreferences will be the most appropriate solution.