Use Application in android

Source: Internet
Author: User

Use Application in android

During android development, we may store some global variables. We 'd better access them in any activity or service of the app. In this case, we can use the application.

One of our applications is called application, so we should understand that only one single-instance application exists in an application, and it is not difficult to think of using this to store global variables, so how is it stored?

First, we create an Application that inherits android. app. Application:

package com.podongfeng.firstapplication.app;import android.app.Application;public class MyApplication extends Application {private Integer allViewInteger;public Integer getAllViewInteger() {return allViewInteger;}public void setAllViewInteger(Integer allViewInteger) {this.allViewInteger = allViewInteger;}}

Then, declare the Application in AndroidManifest. xml, which is similar to declaring Activity.

In fact, AndroidManifest. xml will definitely have a system declared Application, similar to this:

                    
                                 
              
                             

So how can we replace it with our own application?

In fact, you only need to add the android: name attribute to the application tag to point to our custom application:

                    
                                 
              
                             

OK. In this way, we can use getApplicationContext () in the activity to obtain the custom Application.

Wait, isn't it so convenient? If I write some common java methods, I didn't put them in the activity for good code reuse?

Pass the context through a parameter, and then use context to get the Application?

This can certainly be done. However, since the Application is a Singleton, we can think of using the getInstance method in the singleton design mode to get the singleton object. In fact, our MyApplication integrates the Application and can directly override the onCreate method. When the Application is created, the object is assigned to a static member variable. In this way, you can use the static method of MyApplication to obtain this Singleton anywhere:

package com.podongfeng.firstapplication.app;import android.app.Application;public class MyApplication extends Application {private static MyApplication myApplication = null;public static MyApplication getMyApplication() {return myApplication;}private Integer allViewInteger;public Integer getAllViewInteger() {return allViewInteger;}public void setAllViewInteger(Integer allViewInteger) {this.allViewInteger = allViewInteger;}@Overridepublic void onCreate() {super.onCreate();myApplication = this;}}

OK. At present, we only write an available global variable allViewInteger in it, which is enough to illustrate the problem. If you want to save anything, you can store it easily, the following is an example of a global variable set and get in two activities:

package com.podongfeng.firstapplication;import com.podongfeng.firstapplication.app.MyApplication;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends Activity implements OnClickListener {private Button nextBtn;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);MyApplication myApplication = MyApplication.getMyApplication();myApplication.setAllViewInteger(100);nextBtn = (Button) findViewById(R.id.btn_next);nextBtn.setOnClickListener(this);}@Overridepublic 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;}@Overridepublic 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);}@Overridepublic void onClick(View v) {Intent intent = new Intent();intent.setClass(getApplicationContext(), SecActivity.class);startActivity(intent);}}

package com.podongfeng.firstapplication;import com.podongfeng.firstapplication.app.MyApplication;import android.app.Activity;import android.os.Bundle;import android.widget.TextView;public class SecActivity extends Activity {private TextView textView = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activiry_sec);textView = (TextView) findViewById(R.id.tv_sec);textView.setText(String.valueOf(MyApplication.getMyApplication().getAllViewInteger()));}}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.