How to change toggle button state from another activity - android

Here is my question,my app flow is screen1,screen2,screen3.and their content views are like this:
screen1.java-->screen1.xml
screen2.java-->screen3.xml
screen3.java-->screen3.xml
here in screen1 if user clicks on toggle button to is going to screen2 then screen3 in screen3 payment success then only screen1 toggle button should change,how to achieve this,didn't get any idea,plz help me,Thanks.

You can save the state of toggle button in Shared preferences.
Check this : Shared Preferences Android and Example
Hope this helps.

Use a static global array list and save the pressed states at their positions in that list. In the next activities use that array list to set the toggle status of your buttons
This is similarly like saving the checked states of a checkbox in a listview

You can pass button state using Bundle between activities like following
Start activity 2
Intent intent = new Intent(this, Activity2.class);
intent.putExtra(EXTRA_NAME, VALUE);
startActivity(intent);
Get that value in activity 2 like
#Override
protected void onCreate(Bundle savedInstanceState) {
....
boolean value = getIntent().getExtras().getBoolean(EXTRA_VALUE);
}
same like above you can pass is to Activity 3.
Or
You can make a static variable in you Activity 1 and then access that from Activity 3.

You can make that toggle button static and then you can change its state from any activity.
static ToggleButton toggleButton = (ToggleButton)findViewById(R.id.toggle_btn);
but you need to be careful.

You could also pass the toggle state via intents and onActivityResult().
Here is a video tutorial on intents
Here is some a tutorial on onActivityResult

Related

How do I pass data from an EditText in one Activity to a TextView in another activity without switching to the second activity?

I want to send a string variable that is entered into an EditText on ActivityOne to pass the data and be displayed as a TextView on ActivityThree. However, I am having problems as the only solutions to do this that I can find cause the activity to switch to ActivityThree while doing this. I want to avoid this or maybe even send the data to ActivityThree and switch to ActivityTwo all on the click of a button. Any help or redirection to a current solution would be greatly appreciated.
If you want to send data from ActivityOne to ActivityThree by avoiding ActivityTwo then save that data in a static variable then use that variable in ActivityThree to set TextView data.
You can simply do this using SharedPreferences.
Setting EditText values in Preference from ActivityOne:
// EditText
EditText editText = (EditText) findViewById(R.id.editText);
SharedPreferences.Editor editor = getSharedPreferences("Your_Preference_Name", MODE_PRIVATE).edit();
editor.putString("KEY_VALUE", editText.getText().toString());
editor.commit();
In ActivityThree, retrieve value from Preference:
SharedPreferences prefs = getSharedPreferences("Your_Preference_Name", MODE_PRIVATE);
String editTextValue = prefs.getString("KEY_VALUE", null);
// TextView
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(editTextValue);
Hope this will help~
Please use a global variable in Application class and set it's value in ActivityOne and read the same value from ActivityThree.Global variables are available to entire project Activities.
The best way is using input extra. Do this in Activity one
Intent i = new Intent(ActivityOne.this, ActivityThree.class);
i.putExtra("label", "label_text");
startActivity(i);
Then receive the string in Activity three as such:
EditText input = //intialize it in OnCreate
Intent intent = getIntent();
String data = intent.getExtras().getString("label");
input.setText(data);
There are so many ways to do this.. But it heavily depends on what you plan to do with data and your situation..
If you want to display the data in activity three than you can make data persist and later show it when activity three is created or resumed, now:
1- If you want to display the data in activity three and you want the value to persist only in current session you can use a Global Variable or even Static one, if you define the desired value as a static variable inside your activity three than you can easily access it and use it without any need for the activity to be even created:
public ActivityThree extends Activity {
public static String myValue;
2- If you want to display the data in activity three and you want data to persist even if the app is closed you can use SharedPreferences as described here:
https://developer.android.com/training/basics/data-storage/shared-preferences.html
3- If you want to run a background task in activity three on value getting determined you can use LocalBroadcastManager:
https://www.intertech.com/Blog/using-localbroadcastmanager-in-service-to-activity-communications/

Button Click event in android tabgroup

I have used a tabHost in my android application. The main ActivityGroup class has a listview. When a listview item is clicked, it displays the details in the same tab. I have used a simple activity class for the detail view. the detail view contains a save button which sves the data and returns to the listview(main) screen.
My problem is that for the first time the save button is pressed it works as expected but if the list item is selected again and in the details view if save button is pressed, the application throws error.
Please help. thanks in advance.
The error could be caused by many different things. Check to see if you have implemented your Intent to switch between activities correctly. Like so...
/** Called when the user clicks the save button */
public void save(View view) {
Intent intent = new Intent(this, Main.class);
startActivity(intent);
}
If this isn't what you were looking for, can you provide some examples of your code?

Previous button clearing all data from my fields in android activity

i am developing an android application. i have 8 activities in the application. i have a submit button in the last activity. the user can go back to any activity and edit the data before pressing submit.i have two buttons in every activity named as Next and previous. and in the last activity an extra button called Submit is there. I want to come to any previous page from the next page and edit the data and go to submit.
Now my problem is, every time i press Previous my previous-activity data fields are becoming blank..
please help me solving this problem.
Save the state of your activities with bundles. The back button ends the lifecycle of an activity. Here's a link that would guide you through what you should do. Hope this helps.
http://developer.android.com/training/basics/activity-lifecycle/recreating.html
Dont kill your activity while moving to other activity. Also you can set a flag while launching activity whihc will just put your activity to backstack and will be retrieved when relaunch by pressing back. Follow below link for details:
http://developer.android.com/guide/components/tasks-and-back-stack.html
You need to save the state of Activity:
private static final String INPUT1 = "ip1";
private static final String INPUT2 = "ip2";
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
// Save the user's current game state
savedInstanceState.putString(INPUT1, mEditText1.getText().toString());
savedInstanceState.putString(INPUT2, mEditText2.getText().toString());
// Always call the superclass so it can save the view hierarchy state
super.onSaveInstanceState(savedInstanceState);
}
And restore it when Activity starts:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); // Always call the superclass first
mEditText1 = findViewById(.....
mEditText2 = findViewById(.....
// Check whether we're recreating a previously destroyed instance
if (savedInstanceState != null) {
// Restore value of members from saved state
mEditText1.setText(savedInstanceState.getString(INPUT1));
mEditText2.setText(savedInstanceState.getString(INPUT2));
}
}
Also, you can tell EditText and other such Views to save their state individually, by adding android:saveEnabled="true" to their layout:
<EditText
android:id="#android:id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:saveEnabled="true"
android:inputType="text"/>

Android - Physical Back Button on TextEdit erases content

I have a ListActivity with an EditText and a ListView. Content of the ListView changes according to the EditText content. I do the following steps:
1) Fill in something in the EditText
2) Consequently the Listview changes (properly)
3) I scroll the listview
4) I press the back button
At this point I expect to get back to the previous activity, while i happens that the EditText gets erased and consequently the ListView empty.
Is there a way to tell the EditText not to erase itself when Back Button is pressed?
Thanks a lot.
G
You could save the editText in the Shared Preferences. To always have the text saved when you back or when you open again the application.Take a look : Shared Preferences
Like this:
private SharedPreferences pref;
In your SecondClass:
pref.edit().putString("text", Text).commit();
MainClass:
String texto = pref.getString("text", "");
I just want the activity to finish whenever i press back button. Doesn't matter what i was doing and which object had the focus
Here's how you can force a finish of the Activity when back is pressed...
#Override
public void onBackPressed() {
finish();
}
You can use the onPause method to store the EditText value. Either in SharedPrefrence or a Global variable.
#Override
public void onPause() {
super.onPause();
// Get the current value from Edit text and store the value on sharedpref or global variable
}
here inside onPause method you can save the current value of the EditText into shared preference.
alternatively you can overide the back button's event and do the same as below
you can use following method
#Override
public void onBackPressed(){
//your code here to save it to shared pref or global varialble then invoke relevant activity manually
}
to overide the events when the back button is pressed

How to make the back function and set the last contentView

I hawe many view's in my application and now the problem is how to go back from one view to another.
What I could do it by set back Buttons in every view but i would like to use the android back hard button.
I have tried something like this:
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if(keyCode==KeyEvent.KEYCODE_BACK)
{
finish();
}
return false;
}
But the problem is that this will close my application.
Could you please guide me for a proper solution, for example to memorize the last view was set and then to come back to this view or something like this.
Here is the code with which I am changing the view (it's a method in my main activity):
public void CheckIfUserIsLogedIn ()
{
int userPresence = localDatabase.CheckUserPresence();
if (userPresence == 0)
{
setContentView(R.layout.main);
}
else
{
setContentView(R.layout.userlogedon);
}
}
Thank you.
Look!
You are doing this wrong way..
An Activity class should only have on content View. (because it is recommended way and easy to use and implement).
And if you want to go to next View, show it under another separate Activity.
when you will finish it, you will be automatically redirected to previous Activity.
(and you don't need to memorize the Previous View :) )
See here, how to work with Activity Stacks.
I am not sure to understand your problem correctly because Android do all that for you automatically. Once a view is opened when you switch to another view it is paused (on screen but has not focus) or stopped (has no focus)
http://developer.android.com/reference/android/app/Activity.html
If the current view (activity) has been launched by the previous view (activity), pressing the back button will make you "close" the current view and go back to the previous one automatically.
Now two things :
Perhaps your are simply opening all views wihtin the same activity by showing on or off components which is a bad way of doing and is not recommended by android. What you should do is 1 view = 1 activity.
You are thinking like "iPhone/iPad" where you have to implements back buttons in the "views". In android you don't need to do so. Putting the "finish" command in your code at that point seem to close the application which make me think you have programmed as explained in point 1.
Hope it helps
EDIT:
To start a new activity do it like this
startActivity(new Intent(this, MyOtherActivity.class));
you put this in your code where you want to load the new view (activity)
Now if you want to transfer some information between activities you must do something like this :
Intent myIntent; //intent declaration
int aNumber = 10; // info to send to other activity
String aString = "abcd"; // info to send to other activity
// link Intent to the other activity
myIntent = new Intent(view.getContext(), MyOtherActivity.class)
//put the extra info
myIntent.putExtra("myNumber", aNumber);
myIntent.putExtra("myString", aString);
//start the new view/activity
startActivity(myIntent);
and in the new opened activity you retrieve the infos like this (in the oncreate usually)
int aNumber;
String aString;
#Override
public void onCreate(Bundle savedInstanceState) {
aNumber= getIntent().getExtras().getInt("myNumber");
aString= getIntent().getExtras().getString("myString");
}
Actually i m not sure that understand exactly but..
take a map or a shared preference and at the back button set last View on map or Shared preference .
At the calling or at start activity fetch the data which have stored you.
this will helps you.

Categories

Resources