简体   繁体   中英

move bundle without using intents - android

Is there any way in which I can move a bundle of data from one class to another without actually changing the layout?

For example:

I have 3 classes: class A, B and C. Now class B has navigation drawer and bottom navigation menu implemented which can be seen on class A and C and also be used at the same time.

However, I have a button in the bottom navigation menu which takes me to class C but the data which I need to view in this is in class A.

Is there any way by which I can just send a bundle of data to class B but without using intents, then retrieve the data from class B and show it on class C?

** EDIT **

PS : B is an AppCombatActivity extended class and A and C are Fragment extended classes.

I would suggest using a SharedPreferences file to store the data in Class A. Then you can read from the SharedPreferences file wherever you want to show the data. Refer this page - https://developer.android.com/training/basics/data-storage/shared-preferences.html

Your question is very simple to implement but it can also become a headache. This is what you should do.

class A:

public class A extends Activity {

 static A INSTANCE;
 String data="A"; 

 @Override
  public void onCreate(Bundle savedInstanceState) {

    INSTANCE=this; 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
   }

 public static A getActivityInstance()
   {
     return INSTANCE;
   }

 public String getData()
   {
     return this.data;
   }
}

class B:

public class B extends Activity {

 String data; 

 @Override
  public void onCreate(Bundle savedInstanceState) {
    data=A.getActivityInstance().getData();  
   }
}

The trick is to create an Instance in class A and then using that instance to access all the public elements like methods, variables etc using that Instance.

Hope this helps :)

You could use interface for passing data, this the reference

https://stackoverflow.com/a/9977370/2951976

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