简体   繁体   中英

How to access getIntent() from extends AppCompatActivity to extends Application

So, my Activity A is :

public class imei extends AppCompatActivity {
//variables    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_imei);    
        ...    
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                if(Long.parseLong(IMEI) == IMEI2){
                    IMEI .equals(IMEI2);
                    textView.setText("IMEI NUMBER : " + IMEI);
                    Bundle bundle = new Bundle();
                    bundle.putBoolean("key", true);
                    Intent intent = new Intent(imei.this, menu_utama.class);
                    intent.putExtras(bundle);
                    imei.this.startActivity(intent);
                }else{}

now i want to pass value the Boolean value from bundle to Activity B :

public class MyApplication extends Application {
private BeaconManager beaconManager;    
    //variables

    @Override
    public void onCreate() {
        super.onCreate();

        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        final boolean success = bundle.getBoolean("key");    
        ..code
            if(success){
                do something
            }else{do something

but the getIntent() is red, but when i change to extends AppCompatActivity , getIntent() correct. somehow it has to do with onCreate(Bundle savedInstanceState) and extends Application what should i do to fix this

so my problem is how to passing value between activity class to aplication class, first try i'm using Bundle which is using getIntent(), but it did not work because getIntent() is in activity class, because i want to using in application class, so i'm using shared Preference and it works.

SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE);
                    SharedPreferences.Editor editor = pref.edit();
                    editor.putBoolean("masuk", true);
                    // Save the changes in SharedPreferences
                    editor.apply(); // commit changes

put it in activity class and get the shared preferences in application class:

SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE);
        SharedPreferences.Editor editor = pref.edit();
        editor.putBoolean("masuk", true);
        // Save the changes in SharedPreferences
        editor.apply(); // commit changes

and then just use if(masuk){do what you want here}else{do what you want here} Solved.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM