views:

52

answers:

2

hello everyone,

I'm sharing some variables accross activities by using a class like this :

public class Globals {

static Boolean hint1_graph_type_switcher; 
static Boolean hint2_stockview_valuation;

other variables ...
    }

then I'm using these variables anywhere across my multiple activites with ...

if (Globals.hint2_stockview_valuation == false) {

        ....

    }

pretty basic and it was working fine untill ...

I introduced some webview stuff like this:

//-----------------------------------------------------------
        // open a webview with the NEWS when the more_arrow is clicked :
        mNews.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                String news_URL = "http://us.m.yahoo.com/w/yfinance/symbolheadlines/"+ ticker + "/?.intl=us&.lang=en";
                Intent news_Webview_intent = new Intent(Chart_View.this, News_Webview.class);
                news_Webview_intent.putExtra("NEWS_URL", news_URL);
                startActivity(news_Webview_intent);
            }
        });
        //-----------------------------------------------------------

and here's the News_Webview.class:

public class News_Webview extends Activity {

//
// http://www.chrisdanielson.com/tag/progressdialog/
//

String news_URL;
private WebView webview;
private ProgressDialog progressBar;
private static final String TAG = "Hub";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.webview_news);
    this.webview = (WebView)findViewById(R.id.webView);


    Bundle extras = getIntent().getExtras();
    if (this.getIntent().getExtras()!=null){
        news_URL = extras.getString("NEWS_URL");
    }


    WebSettings settings = webview.getSettings();
    settings.setJavaScriptEnabled(true);
    webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);

    final AlertDialog alertDialog = new AlertDialog.Builder(this).create();

    progressBar = ProgressDialog.show(News_Webview.this, "", "Loading...");

    webview.setWebViewClient(new WebViewClient() {

        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            //Log.i(TAG, "Processing webview url click...");
            Intent viewIntent = new Intent("android.intent.action.VIEW", Uri.parse(url));  
            startActivity(viewIntent);
            return true;
        }                  

        public void onPageFinished(WebView view, String url) {
            //Log.i(TAG, "Finished loading URL: " +url);
            if (progressBar.isShowing()) {
                progressBar.dismiss();
            }
        }

        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            Log.e(TAG, "Error: " + description);
            Toast.makeText(News_Webview.this, "Oh no! " + description, Toast.LENGTH_SHORT).show();
            alertDialog.setTitle("Error");
            alertDialog.setMessage(description);
            alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    return;
                }
            });
            alertDialog.show();
        }
    });
    webview.loadUrl(news_URL);
    }
}

the problem now is that, when it "comes back" from this Activity, it looks like my Globals variables have disappeared ???

if (Globals.hint2_stockview_valuation == false) {

fires an error :

06-23 12:14:03.443: ERROR/AndroidRuntime(2611): Caused by: java.lang.NullPointerException

2 questions then :

  • Should I use something else than this "Global" class to share variables across activities ? Is it just bad practice to do this ?? I know that I can use the preferences but I thought it was quicker to do it this way (no need to "read" the preferences everytime I start a new activity ...

  • Any idea on WHY this is happening ? Should I "get back" my savedInstanceState in some way when my activity returns from the News_Webview.class ???

As always, thank you for your help.

H.

+2  A: 

U could use intent.putExtra() for inter-activity interaction...

One thing(just for diagnostics) i would suggest is initializing the static variables to some value and then run the App... i suppose your global class is getting re-initialized when the intent started activity returns back...

I have used global variables, but in my case i kept them in an activity which never died. All other activities came after it and it worked perfectly fine...

JaVadid
I know that, thanks. but I'm also sharing more complex objects using this technique (Global class)... and it does not explain the problem stated here.
Hubert
Indeed it looks like the Activity is re-started when it comes back. Would savedInstanceState save these variables somewhere ? Should I get them back in some way maybe ? How did you force your "activity to never die" ?
Hubert
well you can try to save them in the savedInstanceStatein the onPause(), retrieve them in the onResume() of your activity... guess this might work out...
JaVadid
OK I can do that for the primitive types it's going to be pretty OK but can I do that also with the more complex objects ? Do I need then to save them as Serializable in a file (will slow down the whole thing, isn't it) ?
Hubert
i was supposing you are going to ask that... Ya thats the problem with complex objects.. http://stackoverflow.com/questions/1984682.. as far as this post explains, i can suggest you that u can make the same objects in the activity which calls the intent and populate them with your data in onPause()... Then assign those values to the actual global variables in onResume()... wat say???
JaVadid
I see what you mean, will give it a try...
Hubert
sure... but do remember to deallocate memory from the temporary created variables as soon as u have re-allocated them to the global variables... =)
JaVadid
Hubert
Glad that it helped and GLADer that you got it working! :)
JaVadid
A: 

The NullPointerException is bubbling up from the android runtime and has nothing to do with your Globals type.

ktingle
can you explain a bit ... I don't understand what you mean ?
Hubert
Examine your callstack more closely; my guess is something is happening while loading the 'back' activity. You can confirm this by temporarily commenting out the code that accesses you Global type.
ktingle
i guess most of the NullPointerException are thrown with the AndroidRuntime tag... :)
JaVadid
Accessing static boolean variables will never result in a NullPointerException no matter what the state of the system is.
ktingle