Discussion on:
View:
Show:
I have yet to get a clear answer on this from anyone. I generally just store my vars up in the Application, which, as far as I understand, lives along with the Activities in perpetuity. Why go through all the hassle of packing and unpacking things into the Bundle if the live data is available to you at all times? It also makes for very convenient sharing of data between Activities. What am I overlooking? Is there some danger of memory leak? Or can the application vars be released unexpectedly? (I have ever to see that happen)
In my experience extending an instance of the Application class, and using it to store some global variables, *is* an acceptable approach in various scenarios. Whether it is the right approach depends largely on your application and the specific circumstances. Remember that Android is an embedded and small footprint environment. Keeping track of some application state-wide variables up at the Application object so it can be referenced via multiple activities is probably reasonable. Storing bitmaps being manipulated in an activity probably isn't. The idea being that when a call comes in and your activity is paused or ended to free up other resources, the big chunks of data you are storing in your extended application instance are not freed up. So if you are not responsible in the choices you make in your implementation, then your app can negatively impact the user's phone over all. To my knowledge it *is* possible for the kernel to kill your entire application under extreme resource starved conditions, but like you I've yet to see it happen. So I guess the short answer to your question is I see nothing wrong with your approach, so long as it is used with discretion. At the same time, I don't see packing and unpacking activity level variables as a major hassle or concession, and, I see scenarios where doing so would be beneficial over your approach (such as those I discussed earlier where the variable I am trying to keep up with is large enough to have a negative impact on the OS overall, or, as I will discuss in the future, when the variable is a pointer to an activity specific async task that is performing long running i/o on a background thread). I hope that answers your question. And thanks again for taking the time to share it. You made a really good point.
package com.example.helloandroid;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Button;
import android.widget.Toast;
public class HelloAndroid1 extends Activity {
/** Called when the activity is first created. */
private EditText text1;
private EditText text2;
private EditText text3;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
text1 = (EditText) findViewById(R.id.txt1);
text2 = (EditText) findViewById(R.id.txt2);
text3 = (EditText) findViewById(R.id.txt3);
}
public void myClickHandler(View view) {
switch (view.getId()) {
case R.id.btn1:
Button btn1=(Button)findViewById(R.id.btn1);
int inputValue7 = Integer.parseInt(text1.getText().toString());
int inputValue8 = Integer.parseInt(text2.getText().toString());
if (btn1.isClickable()) {
text3.setText(String
.valueOf(division(inputValue7,inputValue8)));
break;
}
case R.id.bnt2:
Button btn2=(Button)findViewById(R.id.bnt2);
int inputValue1 = Integer.parseInt(text1.getText().toString());
int inputValue2 = Integer.parseInt(text2.getText().toString());
if (btn2.isClickable()) {
text3.setText(String
.valueOf(addition(inputValue1,inputValue2)));
break;
}
case R.id.btn3:
Button btn3=(Button)findViewById(R.id.btn3);
int inputValue3 = Integer.parseInt(text1.getText().toString());
int inputValue4 = Integer.parseInt(text2.getText().toString());
if (btn3.isClickable()) {
text3.setText(String
.valueOf(substraction(inputValue3,inputValue4)));
}
break;
case R.id.btn4:
Button btn4=(Button)findViewById(R.id.btn4);
int inputValue5 = Integer.parseInt(text1.getText().toString());
int inputValue6 = Integer.parseInt(text2.getText().toString());
if (btn4.isClickable()) {
text3.setText(String
.valueOf(multiply(inputValue5,inputValue6)));
}
break;
}
}
private int addition(int l,int m)
{
return(l+m);
}
private int multiply(int l,int m)
{
return(l*m);
}
private int division(int l,int m)
{
return(l/m);
}
private int substraction(int l,int m)
{
return(l-m);
}
}
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Button;
import android.widget.Toast;
public class HelloAndroid1 extends Activity {
/** Called when the activity is first created. */
private EditText text1;
private EditText text2;
private EditText text3;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
text1 = (EditText) findViewById(R.id.txt1);
text2 = (EditText) findViewById(R.id.txt2);
text3 = (EditText) findViewById(R.id.txt3);
}
public void myClickHandler(View view) {
switch (view.getId()) {
case R.id.btn1:
Button btn1=(Button)findViewById(R.id.btn1);
int inputValue7 = Integer.parseInt(text1.getText().toString());
int inputValue8 = Integer.parseInt(text2.getText().toString());
if (btn1.isClickable()) {
text3.setText(String
.valueOf(division(inputValue7,inputValue8)));
break;
}
case R.id.bnt2:
Button btn2=(Button)findViewById(R.id.bnt2);
int inputValue1 = Integer.parseInt(text1.getText().toString());
int inputValue2 = Integer.parseInt(text2.getText().toString());
if (btn2.isClickable()) {
text3.setText(String
.valueOf(addition(inputValue1,inputValue2)));
break;
}
case R.id.btn3:
Button btn3=(Button)findViewById(R.id.btn3);
int inputValue3 = Integer.parseInt(text1.getText().toString());
int inputValue4 = Integer.parseInt(text2.getText().toString());
if (btn3.isClickable()) {
text3.setText(String
.valueOf(substraction(inputValue3,inputValue4)));
}
break;
case R.id.btn4:
Button btn4=(Button)findViewById(R.id.btn4);
int inputValue5 = Integer.parseInt(text1.getText().toString());
int inputValue6 = Integer.parseInt(text2.getText().toString());
if (btn4.isClickable()) {
text3.setText(String
.valueOf(multiply(inputValue5,inputValue6)));
}
break;
}
}
private int addition(int l,int m)
{
return(l+m);
}
private int multiply(int l,int m)
{
return(l*m);
}
private int division(int l,int m)
{
return(l/m);
}
private int substraction(int l,int m)
{
return(l-m);
}
}
- Keyboard Shortcuts:
- Prev
- Next
- Toggle

































