I need to transfer my Activity , that is Context , through the Bundle . How to put her there?

  • 6
    context cannot be passed through the Bundle . If you need it, then you are doing everything wrong. It seems that we have already agreed that you will not produce questions with “ideologically” wrong approaches that you come up with yourself, but will instead ask about the problem itself. - pavlofff
  • 3
    if you can do this, you will not be able to take advantage of the context. You are obviously trying to make some awful crutch. It is better to ask what you are trying to achieve, and not how to implement your crutch. - Vladyslav Matviienko
  • @metalurgus, but the problem is to describe the whole problem, you need to throw off the floor of the project. But still, I was a brake and did not notice one error, because of what I tried to launch the program with a crutch, but when I wrote the crutch I did not notice another error (for which I wanted to throw Activity through Bundle ). But then I still added a crutch! True, I forgot to initialize the context and gave me an NPE. In search of a mistake, I realized that it turned out to be crap, I started writing everything all over again - everything turned out neat and clean (even without crutches :)). - user189127
  • one
    @metalurgus; It turned out that the variable needs to be registered not via getActivity().registerForContextMenu , a simple registerForContextMenu , но вызывать контекстное меню нужно было через called , но вызывать контекстное меню нужно было через getActivity.openContextMenu` (which is why I got confused). Now everything is good! I feel Yuri, pavlofff, you and many more will be very happy when I add this application and still read the book: D. - user189127 1:54
  • @pavlofff, the messages above are also for you (I just could not send them to two at once). - user189127

2 answers 2

No Bundle intended for storing serialized data in the form of a key-value.

Describe why you need it and we will tell you how to do it. Options may be:

  1. Pass the Context via a method / constructor.
  2. Get Context with View.getContext() method of any widget.
  3. For a fragment, you can use the getActivity() method.
  4. You can make a singleton class Application and get it Context .
  5. In the case of communication activation service, use Binder
  • And what, activation cannot be value? Not serialized? - Nick Volynkin
  • one
    @NickVolynkin, well, in terms I am not strong, but, as far as I understand, activating is a process, we plant it and not this ... not to save as a data set ... If in something I am wrong, correct it in the comments or answer itself) - YuriySPb
  • four
    @NickVolynkin It’s not worth even thinking about the possibility of transmitting activations (context) through a bundle, it’s not only beyond evil, but also not in this universe at all. - pavlofff
  • I solved the problem, but theoretically, can I somehow change the Context into a data type of type int , string and the like, and then back? (The data types are EXCLUSIVELY for example). - user189127 1:58

Alternatively, you can get the application context:

 import android.content.Context; public class ApplicationContext { private static Context context; public static Context getAppContext() { return ApplicationContext.context; } public void onCreate() { super.onCreate(); ApplicationContext.context = getApplicationContext(); } } 

And in the manifest in the <application block, <application add android:name=".ApplicationContext"

And we call this context like this:

 ApplicationContext.getAppContext() 

And if you want to activate the context in the fragment , you can do this:

 private AppCompatActivity act; @Override public void onAttach(Context context) { super.onAttach(context); act = (AppCompatActivity) context; }