From my activity I call showDialog(0) and then in the switch of the method onCreateDialog I would like to access a variable from the parent activity. It doesn't seem to work though. I've tried
this.neededVariable
or
((MyActivity)dialog.getOwnerActivity()).neededVariable
but they don't seem to work. Any ideas? It might be something simple which I'm not getting cause I'm very tired :)
you can pass the value of that variable to your dialog in setArgumenet, or you can use Fragment Callback to communicate the Activity, here is the example of callback
When you are in dialog you should use dialog context to interact with your dialog and when you want your Activity context you can use your activity name like :
YourActivityName.this
here is a sample that i've used in dialog code to access a TextView inside of the parent activity :
((TextView)DayReportActivity.this.findViewById(R.id.edtDailyCPCode)).setText(CPCodeText);
Related
In my fragment I have a 'dismiss' button that should behave in a different way dependent on which Activity called its parent activity (say TutorialActivity).
In the TutorialActivity I am already determining which Activity called it. How to pass that data down to the fragment?
My fragments reside in a PagerAdapter and I wouldn't like to need to pass this info as a 'newInstance()' parameter every time, as it seems an overkill, taking into consideration that this parameter would be the same for each fragment in my FragmentStatePagerAdapter.
You can access the variable from Tutorial Activity by making this variable public. Suppose variable name is parent. You can access it by using instance of TutorailActivity (suppose instance of Activity used in fragment is mTutorialActivity) then it should be like mTutorialActivity.parent.
But you need to pass instance of TutorialActivity as it may be used for other purposes also like fetching strings from strings.xml and other purpose. So it would be beneficial to pass Activity instance instead of variable.
What I ended up doing: I implemented said "different behavior" in the TutorialActivity itself, having moved the dismiss button up from the fragment. Then a simple onClickListener and a switch statement inside of it.
I'm badly confused and hope to get your help understanding this concept.
I have an app with 3 activities, splash, login and main and the main activity is a multi fragment drawer activity that uses sqlite.
In my fragments i need to use context many places. I used to have a static context in my main activity defined and passed that around but in another questions someone suggested not to use static context to avoid leaks and i took the advice.
I had to change a few things and got things working. I use getapplicationcontext() but now my application now consistently crashes. The crashes are more prominent when the app is put in background.
My question is, which activity's context should i be using to start with? Splash? Login or main? How would you get access to the context in resume?
Thanks
If you are in A fragment you can use getActivity() to access its container activity context.
If you are try to access activity context from an adapter class or dialog, you must pass the activity context to the constructor of the adapter class or dialog
If you are in an activity you can use this or yourActivity.this as per the situation.
For example
1)If you want to access activity context from onResume() or onCreate() you can simply use this.
2)If you are try to access activity context from an inner class like retrofit call then you must use yourActivity.this for activiy context
I'm facing an issue which hopefully you can help me to fix. In my Android app, I display a Snackbar using the usual way (Snackbar.make().show()). However, I also have to deal with configuration changes (like orientation changes from portrait to landscape).
Now if the configuration changes, the Snackbar disappears, which is nothing I wouldn't expect. I had the same issue with ProgressDialog and AlertDialog, which I solved by using a custom subclass of DialogFragment.
I also had this issue with my AsyncTasks which I solved by using a custom subclass of Fragment which I retain (setRetainInstance(true) in onCreate() of the Fragment) and which keeps a reference to my AsyncTask. In both cases, each Fragment has a callback interface which the Activity has to implement and I set/unset the callback during onAttach()/onDetach().
However, Snackbar seems to be different because it is attached to a View instead of a Context like for example the ProgressDialog (in getDialog() of my DialogFragment I can use new ProgressDialog(getActivity())).
So when and how shall I display a Snackbar after a configuration change? Is using a Fragment the way to go or should I try to work around this by having some private variables on my Activity which I save/restore via the savedInstanceState?
Since no suitable answer was provided and I had to meet a deadline, I now went with the following solution:
Create a activiy base class which extends AppCompatAcitity and from which all my activies inherit
In the activity base class, create a bunch of instance variables to store information about the Snackbar (isShowing, identifier, message, action, arguments)
In the activity base class, create a method to show the snackbar, giving it all the previously mentioned variables
In the activity base class, use onSaveInstanceState and onRestoreInstanceState to save/restore the previously mentioned variables
If the Snackbar was showing before the configuration change, the boolean will be saved and restored and I can use it to create a new Snackbar with the same configuration in onRestore() of my activity base class
When creating the snackbar, I use the snackbar.setCallback() to set a callback where I check if the snackbar was dismissed and if so, I change the isShowing to false
When setting up the snackbar, I set the action to call a method in my activiy base class using the identifier I previously provided as an argument to check which Snackbar is was clicked
I have an activity which has a static method for updating a textfield. This way I can update this view from another activity.
But now I'm trying to get a Context variable in this static method which is not possible. I've tried declaring a Context variable and initialising it in onCreate ( context = getApplicationContext();)
But still I can't access context in this static method. How is this normally done?
edit: a little bit more information about my situation. I'm starting a countdowntimer in an activity(a) which updates another activity's(b) ``textfield every second. And it does this by accessing b's setTextField in a static way..
How is this normally done?
Accessing a TextView via a static method is not the best way to update the field from another activity. If you want to pass a value to the activity when it starts, you can send data via the intent (i.e. intent.getExtras). If you want to pass data back from a sub-activity, you can use startActivityForResult.
The way you are going is very strange. Why are you trying to change one activity content from another? May be you need to use startActivityForResult to strat a new activity and then return result from it and change views depending on it?
You might want to check some documentation on OO and using static functions. It is not considered a very good approach.
But as we are not talking about a better complete sollution: you can add a parameter with a context to the function, and just give it when you call the function :)
I would suggest the LocalBinder pattern to update the other Activity:
http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/LocalService.html
Can you do something like this?
something like this <viewobj>.getContext()
Ref: How can I start an Activity from a non-Activity class?
Whenever you're busy with Activity A, there's no point in updating something on Activity B as it is simply not shown to the user at that point in time.
Seems to me you need to have some kind of global variable here that can be picked up in the onResume of Activity B.
Checkout this question : How to declare global variables in Android?
It shows you how to use the Application class to maintain global application state, accessable from all activities when needed.
I have Activity A which defines a dialog using AlertBuilder.create etc. This Activity invokes the dialog using showDialog(dialogID). dialogID is declared and recognized in all classes/activities. Everything works perfectly in Activity A.
My question is when trying to invoke this same dialog -- showDialog(dialogID) -- from another Activity (Activity B) the application crashes. Can anyone help?
How to invoke a dialog from multiple activities?
Thanks in advance.
Andy
My question is when trying to invoke
this same dialog --
showDialog(dialogID) -- from another
Activity (Activity B) the application
crashes.
How do you invoke the dialog from Activity B? Just to let you know, you do not instiante activities (so no new ActivityA().showDialog(id).
What you can do is
Create a class that extends AlertDialog.Builder and accept a Context parameter in the constructor. You can customize the text, buttons and other things.
From your activity, in your onCreateDialog, you can just instantiate your class and call create() on it. And your class will be accessible from any activities.
I don't think you can accomplish what you want without hooking up a similar entry point in your new activity.
showDialog(int id)
The id is unique within the activity that launches it. So if two activties A and B both call showDialog(1); That will do something different in each unless someone has coded the same code path for them in their onDialogCreate() and onPrepareDialog methods. So in your onDialogCreate of the original activity, that code will have to exist in both activities. You can sometimes get way with creating a new Dialog type that does all the initialization internally based on a given context and just call show() on it. The problem with this solution usually arises when the context is no longer valid and you need to dismiss or show it. Basically though when using showDialog() it's on a per activity basis.
I don't think that's possible.
Dialog built in Activity A, belongs to Activity A.
No matter if you store its ID in a global data space. It won't be available to be used in Activity B.
You'll have to create another Dialog in Activity B