Static Keyword , Reinstallation Required - android

In my application I have an Activity" Model" in which i get a value 'n' from an edit text , I have declared this value n as static int . So , that i can access it inside any class of the Application.
The problem is that when I restart my application without reinstalling it , the value of 'n' remains the same as it was in the first case . And this affects my output.
I cannot use intent to send values because , the value is accessed randomly in the application even in classes that are not activities.
Can u please tell , where I m wrong.??
package com.integrated.mpr;
import java.io.File;
import android.app.Activity;
import android.app.Dialog;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Model extends Activity implements OnClickListener{
EditText etPos;
Button bmodel;
static int n;//static variable to be used in other classes
File folder ;
File subfolder;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.inputpage);
etPos = (EditText) findViewById(R.id.etpos);
bmodel = (Button) findViewById(R.id.bModel);
bmodel.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.bModel:
String check = etPos.getText().toString();
String check1 = etNs.getText().toString();
n = Integer.parseInt(check);
Intent openAlternative = new Intent("com.integrated.mpr.ALTERNATIVE");
startActivity(openAlternative);
break;
}
}
}
If i first install my app , and enter value in the edittext as 2 , the value of n =2 ; If second time i run my app without installing it , even if i enter 3 in the edittext , value of n remains 2
Where is the twist??

Kumar,
The behavior you are seeing is the result of using a static member. static members do not require an instance and are therefore set upon first access (regardless of whether an instance was created or not) and stays in memory until Android decides it is no longer valid. In essence, it is doing exactly what it was supposed to. The proper use of static variables is a topic of extensive discussion among veteran and novice programmers alike, but essentially always leads to "be choosy about where and how you use static members".
That said, your need to access this from another component is a common problem, however, and there are a number of ways to solve it. If you need to have each instance have a different value, then it should not be static. Instead you will have to find a way to pass the instance of the Activity.
If each instance need not be different, but the value need to change according to some other parameter, simply find the appropriate place to change the value. If you can access it from anywhere in your application, you may also change it from anywhere in your application.
Solution 1: Passing by Intent
This solution is useful when the information is subject to change and must be sent to another component and the classes that it uses exlusively. You may pass virtually any value via an Intent extra.
openAlternative.putExtra("MyValue", Integer.parseInt(check));
In your responding component, you may retrieve the value by:
Intent myIntent = getIntent();
int n = myIntent.getIntExtra("MyValue", 0); //0 is the default if no value is sent.
From here, you may easily pass the retrieved value to any class being utilized by that component. An example:
MyClass.setN(n);
Solution 2: Storing outside of the LifeCycle
A safer alternative is to move the value to an extended Application. This is not subject to UI or LifeCycle processing.
public class MyApplication extends Application
{
static int n;
}
Adjust your AndroidManifest.xml...
<application android:name=".MyApplication" ... >
<!-- All of your components -->
</application>
Now, you can set the variable this way:
MyApplication.n = Integer.parseInt();
And you can get it by
int myN = MyApplication.n;
This solution has gotten me through many a troubled day. However, it should really be used for non-instance related data.
Solution 3: The REALLY UNSAFE method
This solution only works if you can guarantee a single instance of the component. This requires that singleTask is set. Be very careful with this
Change n to non-static
int n;
Change Activity to Singleton
static private Model myInstance;
In OnCreate, set myInstance
myInstance = this;
Create a getter:
static public Model getStaticInstance()
{
return myInstance;
}
This is unreliable (at best) and can cause huge memory leaks if not managed correctly.
Hope this helps,
FuzzicalLogic

Related

Creating Object from other class

I have a settings screen where you can choose between, add and remove configurations for the app.
When adding a configuration, I create a new Instance of a inputBox Class (extending the settings activity class - where I stored the procedure for the standard android text input box) to query the name for the new configuration.
In the Onclick of this inputbox a procedure from the superClass (the settings-activity) is called to create a new configuration object.
This Procedure queries some things from the activity (e.g. selected spinner element) including the progress of a seekBar.
This is where I get a NPE:
java.lang.NullPointerException: Attempt to invoke virtual method'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
The same object creation procedure is also called on initialization of the app and works just fine.
I understand from the Error that the issue is that when calling the procedure from a child class the reference of the variables to the corrseponding elements of the screen is not set anymore - and therefore cannot be queried.
So the question:
How can I query values of activity elements, when the procedure is called from another class?
I know that the topic is quite broad, but I can't figure it out for a couple of days now
Thanks for your help in advance.
Here is a scheme of the problem:
public class Settings extends AppCompatActivity{
Context settingsContext = this;
private Spinner someSpinner;
//other elements
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
someSpinner = (Spinner) findViewById(R.id.someView);
//other elements
addNewConfig.setOnClickListener((v) --> {
inputBox inputBox = new inputBox("OK", "Cancel", settingsContext, "sourcePath",1,1);
newConfigName = inputBox.show();
});
public sSetting makeNewConfig(String name, String sourcePath, int dataFrom, int dataTo){
sSetting newConfig;
newConfig = new sSetting("NAME", someSpinner.getProgress()>0, ...);
return newConfig;
}
}
And the inputBox:
public final class inputBox extends Settings {
//someVars
inputBox(String buttonOk, String buttonCancel, Context setContext, String sourcePath, int dataFrom, int dataTo){
//variable setters
}
private String show() {
//show msgbox
//onclick ok
super.makeNewConfig(....);
}
For solving the problem I restructured my Project a little:
I removed the inputBox-Part, which, after some research considered for a too complicated solution for what I needed anyway.
However: I now added a editText to my Settings View.
Although I had to change my Settings view for this, it now looks better and it was ridiculously easy to edit the configuration name for the user.
I think in most cases that will do the trick. Adding Popup-Boxes just needs more error handling and makes the design more complicated
I hope this helps ;)
If you need the code for it it is available here:
GitHub - AIM

Store a string until the application gets "force stopped"

I need a way to store a json-string as for as long as the application is alive.
So to put this into other words :)
If the application changes orientation or gets paused or stopped i need to store my value/string..
But if the application gets forced killed for some reason I want the value to be removed/droped
Im trying to create a custom history-handler.. and this history handler is suppoed to keep history of actions with in my application for as long as the application process is actually running, but as soon as the process gets killed I need the history to get erased as well.
I have tried to store the value/string as a SharedPrefernce, and to simply remove it once isFinishing()== true
I have also tried using the onDestroy as well.. but there seems to be no "beforeProcessKilled"-event to listen for.. so maybe there is another option than using the SharedPreferences?
Thanks in advance!
Use a simple Java static data member. That value will live until the process is terminated, for whatever reason.
It's my way, maybe not the best.
You can create a class named "Global" or other...
Then declare inside something like this, some "static" variables :
public class Global_variables {
public static JSONArray test;
public static String test1;
}
Then, in your activity or other, import "Global_variables" and get your String or other like this:
String IwantTostore = Global_variable.test1;
If you change orientation, your Global_variable.test1 keep alive ;)
Hope this help
I usually do something like this
public class Globals extends Application {
private String jsonString;
public void jsonString(String string) {
jsonString = string;
}
public String jsonString() {
return jsonString;
}
}
then access it with something like
Globals myGlobal = (Globals)getApplication();
// get json string
String json = myGlobal.jsonString();
so on...

Keeping a session in all application life

I need to create a session and change it at times. In a specific activity should recover it and compare it to a different variable and modify the value of this session. I tried to create a class for this, but the change of activity, the value back to null. I need it to remain until the application is closed.
below:
import android.app.Application;
public class Util extends Application {
private static String idCorrente;
public void onCreate() {
super.onCreate();
idCorrente="0";
}
public static String getIdCorrente() {
return idCorrente;
}
public static void setIdCorrente(String id) {
Util.idCorrente = id;
}
}
I do not know exactly the right way to do it.
You need to store the data on the device somehow. I would recommend reading the Storage Options page of the Android Developers Guide.
Specifically, I think you will find SharedPreferences well-suited for your application.

Using a variable which has been changed in an if statement

I built two activities and the MainActivity is passing some variables to the activity "Calculation". This works as intended and the variables are submitted and received correctly. I now want to create the integer "size_int" depending on the values of the intent "size". The problem occurs in this line:
debug1.setText(size_int);
Eclipse tells me that I should create a local variable with the name "size_int". I do not understand why "size_int" can not be used in this line because it has been defined in the if statement before. Do you have any ideas on that? I assume it has something to do that the variable "size_int" is being defined in the if statement but I am not sure.
Here is the full code:
package com.example.eggtimer;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class Calculation extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.calculation);
// get Intents (Size, Temperature and yolk from Main Activity)
String size = getIntent().getExtras().getString("size");
String temperature = getIntent().getExtras().getString("temperature");
String yolk = getIntent().getExtras().getString("yolk");
if (size.equals("Small")) {
int size_int = 30;
}
// Debug Variables
TextView debug1 = (TextView) findViewById(R.id.textViewDebug1);
debug1.setText(size_int);
}
}
Change like below. This is because if you declare inside the braces, the scope is restricted, so you need to increase scope by declaring outside
int size_int = 0;
if (size.equals("Small")) {
size_int = 30;
}
You need to read about concept called "variable scope". In general, variable declared in code block is local to that code block and is NOT visible outside. Therefore you should declare your size_int outside your if():
int size_int = 0;
if (size.equals("Small")) {
size_int = 30;
}
In general, variables declared outside code blocks are visible in the blocks, while variables declared inside code block are local to that code block.

SharedPreferences and Application class

I have many shared preference for my app (mostly relating to color customization) and I'm unsure what the best method is to store/use them at runtime.
Currently I am doing something like this (with more or less preferences depending on the view) in every activity/fragment:
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getActivity());
int buttonbg = settings.getInt("buttonmenu_bg", 0);
int buttontxt = settings.getInt("buttonmenu_txt", 0);
int headerclr = settings.getInt("header", 0);
And then using those to set the various colors in the display. This seems like a lot of overhead to have to call the PreferenceManager each time and go through all that.
So I started looking at creating an application class, reading the preferences in once and using static variables from the application class in the activities/fragment to set the display.
My question is, are there any drawbacks or gotchas to doing this that I should consider before I venture further down the Application class path?
If you are not using so many static variables so this may not affect your application.But the problem with static variable may arise when your app goes to background and the app running on front requires memory so it may clear your static data,so when you will go to your app you may find nothing (null) in place of static data.
The purpose of the Application class is to store global application state or data (in memory of course), so your approach is correct. I've used it multiple times and it works like a charm.
What I usually do is to create a Map member variable and provide methods for getting and putting values into it, looks like this:
package com.test;
...
...
public class MyApp extends Application{
private Map<String, Object> mData;
#Override
public void onCreate() {
super.onCreate();
mData = new HashMap<String, Object>();
}
public Object get(String key){
return mData.get(key);
}
public void put(String key,Object value){
mData.put(key, value);
}
}
Then from my activities, I just do ((MyApp) getApplication()).get("key") or ((MyApp) getApplication()).put("key",object). Also, don't forget to set the android:name attribute in your manifest file, under the application tag:
<application
...
...
android:name="com.test.MyApp">
</application>
Is there any particular reason why you are not setting the display colors in res/values/styles.xml?

Categories

Resources