I want to toggle a SwitchPreference from another activity (Main Activity).
I have an activity where I ask the user if they want to enable notifications. It contains two buttons and they either click yes or no, as shown below:
I want the answer to be saved and the SwitchPreference to either be toggled, On for yes or off for no. (as well as the SwitchPreference state)
I'm really stuck on how to do this.
Any advice or answers would be most appreciated?
You've got a couple options.
You could do as #0X0nosugar suggested where you simply store the new setting value in SharedPreferences and check the value when resuming the previous activity.
You could use startActivityForResult(intent, SOME_CONSTANT_IDENTIFIER) (vs just startActivity(intent)) in order to have your settings activity return a result to your calling activity. The concept here is that you're starting a new activity which will return a result back to the previous activity. You could then check the value, store it where needed, and update any relevant UI items. Here's an article on how this works https://developer.android.com/training/basics/intents/result.
Related
I am developing an Android Kotlin application which uses two different activities. In each one I have a button that allow me to move to the other one, to say, in Activity 1 I have a button that calls the follwing:
val intentActivity2 = Intent(this, Activity2::class.java)
startActivity(intentActivity2)
This launches correctly Activity2, which similarly inside it I have another button that calls the first activity to return to it:
val intentActivity1 = Intent(this, Activity1::class.java)
startActivity(intentActivity1)
The problem I have is that I want to have both activities running simultaneously (not needed to be shown at screen at the same time), and the issue right now is that every time I call the "startActivity(intent)" a new activity is created, loosing what I had in the previous one, so when I return to Activity1 everything is reset and the same when I go once again to Activity2. Both activities work fine and do their work, the problem is that I can't freely conmute between them.
Is there a way to have both activities at the same time or to not start a new activity everytime I want to change to the other one?
Thank you,
As someone just said you need ViewModel to retrieve your data after deleting/creating new activities. Maybe you can use fragments to do your things. Fragments are attached to activities and it is easier to use them instead of ViewModel.
I have two activtiy. When a boolean variable in activity 1 is true then the UI in activity 2 should be update (activity 2 is with fragment). When I back to activity 2 with (setDisplayHomeAsUpEnabled) it works correctly but with back button it does not work. What is the difference between this ways and how I can solve this. I try update the activity 2 with this code but it does not work:
#Override
public void onBackPressed() {
super.onBackPressed();
//update();
}
The Home Button is not meant to behave like the Back Button. See this reference. The Home or "Up" Button should take the user up within the view hierarchy (i.e. if you have a number of multiple choice fragments, don't go back through each of them, but back to the activity that started the first fragment)
Each activity should specify what it's parent activity is in order to properly use this functionality. You can do this through the manifest, or by overriding onOptionsItemSelected() as outlined here
In terms of why your activity 2 may be updating when using the "Up" button but not the back button, this could be because both handle the back stack in a different way. Back will take you to the last thing on the stack and resume it, assuming you don't haven't tagged the activity with a launchmode that alters this behavior. Here's more info on the back stack. If you haven't designated your Activity 2 as "singletop" then when you use the "Up" Button, a new instance of that parent activity is created. See here. This may means that the information is updating after using the home button because it's creating a new instance of the activity, while for the back button - you are not creating a new instance but resuming a previous instance... make sure you implement an onResume() function to properly handle this and update the information.
The basic navigation of my app is a tab bar within one activity in which each tab is its own fragment. I am trying to get the back button to work to go back to the last tab it was on. I can get the back button to work/appear if the activities are going to the first tab by using this in the java class of the activity:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
and this in the AndroidManifest.xml:
<activity android:name=".CreateNewPost" android:parentActivityName=".MainActivity"></activity>
This works fine, however when I go to a new activity from a button within the other tabs, if I use this same method it goes to tab1. How do I tell it to go to a specific tab or the last active fragment?
Any suggestions would help!
When you launch a new activity the old one either paused or stopped , as in either OnPause() or OnStop() method has been called. Then when you navigate back to a previous activity it is either started (OnStart()) or resuemd (OnResume()). And sometime the activity is destroyed and created again when navigating back to it using OnDestroy() and OnCreate() respectively.
The most guaranteed way that I can think of is to store the state of the first activity somewhere persistently and when the user navigates back to it check what has been stored and show that fragment.
You can use SharedPreferences to store this kind of data, see the google developer documentation on that here. Or onSaveInstanceState(Bundle savedInstanceState) check out this SO question on saving activity state. Although from the answers it is clear that onSaveInstanceState should not work in your specific case, but you should look at it any way because it is useful in other cases like changing screen orientation for example.
My problem is that I have two classes that extend Fragment.Now I have a button(its name is save) in 1 fragment class.I want to add a new button in another fragment class when 'save' button is clicked.I know I need to have an onClickListener for the 'save' button but I don't know how to go further from there.I also want an onClickListener for the new created button.
Any help would be much appreciated.
use interface to communicate from one fragment to another.
follow the below link. You will find out something:
onItemClickListener between two fragments
There are a number of ways to do this, depending on the relationships between fragments, whether they are nested etc.
1) Use SharedPreferences. That means you would write to the apps defaultSharedPreferences some flag which says "save has been pressed", and then in the other fragment any time you call createView you would check this flag in preferences. IF save has been pressed you would then show the button.
This approach has a few issues though depending on how long you want to show this button for, if it should be shown forever etc.
2) The interface approach mentioned is valid, but it has coupling issues, and may not be suited to the framework you have in place.
3) Broadcasts - you can use intents and send messages between fragments. This runs into some vaugeness issues (You need to be careful when documenting broadcasts and intents) and can be somewhat opaque to other readers.
Currently I'm experiencing a bug where if the user quickly taps a button, the intent that the button is attached to will fire off multiple times, resulting in a stack of that intent that will need to be back traced through again. How can I avoid this or remedy this?
Thanks
~k
This is inside of the onClickListener. I set the boolean value here, then I unset it at the end of the process.
if(!isDating)
{
intent.setClass(context, EventDate.class);
isDating = true;
((TabGroupActivity)
context).startChildActivity("EventDate",intent);
}
Try setting the flags for the intent like
intent.setFlags(FLAG_ACTIVITY_BROUGHT_TO_FRONT);
You can also set this flag via AndroidManifest.xml file in the Application section. Prefer this method over above one.
Updating launchMode using the Manifest file
Hope this resolves your issue.
If i had a dime for this bug my QA filed I would be pretty rich if not a millionaire :P There are only so much one can do. Based on your implementation you could try a few things.
As already mentioned, use a boolean. Set it to true once you click it and check this boolean if the button is clicked again. Set it to false once you are done.
Use a progress dialog in case its a long activity like receiving data in your next activity before it displays. This also gives a hint to the user, that something is happening and he need not worry.
use
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
it fixes the problem
Actually I found a better solution!
by setting the onClickListener(null); then recreating it onResume, it bypasses having to use flags and what not.
!k