1

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 **

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

Ali Nasir
  • 217
  • 2
  • 12
  • I strongly recommend that you edit your question and replace most references to "class" with the specific sort of class they are. We have no idea if A, B, and C are activities, fragments, or something else. – CommonsWare Mar 13 '17 at 21:03
  • @CommonsWare done ... check it out – Ali Nasir Mar 13 '17 at 21:06
  • Have A call a method on B to pass data to B (via `getActivity()`). Have B call a method on C to pass data to C. – CommonsWare Mar 13 '17 at 21:07
  • How is that done? Can you kindly show it to me by giving a different answer and not a comment ? – Ali Nasir Mar 13 '17 at 21:08
  • See [this sample app](https://github.com/commonsguy/cw-omnibus/tree/master/LargeScreen/EU4You). – CommonsWare Mar 13 '17 at 21:11
  • @CommonsWare what should I look for in this app? any specifics? – Ali Nasir Mar 13 '17 at 21:12
  • Download the latest [Creative Commons-licensed edition of my book](https://commonsware.com/Android/4-2-free) (currently Version 5.8). That sample app, and hundreds of others, is covered in there. In Version 5.8, this sample is covered starting on page 716. – CommonsWare Mar 13 '17 at 21:15
  • @CommonsWare alright I will check this out – Ali Nasir Mar 13 '17 at 21:19

3 Answers3

1

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

1

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 :)

Saim Mahmood
  • 426
  • 2
  • 12
  • thanks bro, you really are very good at these things man, thank you so much for your time it means a lot to me :) – Ali Nasir Mar 14 '17 at 20:46
  • your welcome anytime bro, always happy to help a fellow developer :D and btw do a thorough research before asking a question. Your question is the same as this guy http://stackoverflow.com/questions/13515842/how-to-pass-data-from-one-activity-to-another-without-using-intents – Saim Mahmood Mar 14 '17 at 20:50
0

You could use interface for passing data, this the reference

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

Community
  • 1
  • 1