I need to transfer my Activity , that is Context , through the Bundle . How to put her there?
|
2 answers
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:
- Pass the
Contextvia a method / constructor. - Get
ContextwithView.getContext()method of any widget. - For a fragment, you can use the
getActivity()method. - You can make a singleton class
Applicationand get itContext. - 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
Contextinto a data type of typeint,stringand 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; } |
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. - pavlofffActivitythroughBundle). 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 :)). - user189127getActivity().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