Pass updated data to home activity without refreshing any activity - android

Here I have 3 activity and ActivityA contain textView which show username , ActivityB contain only button to redirect ActivityC, ActivityC Contain EditText and save button.
Now i want to change updated text from ActivityC to direct ActivityA when save button clicked without refreshing any Activity than what i have to do? can you please suggest simple way.
I have a same requirement in complex project, i need to save user location at server side using api call and show updated data in ActivityA.

Another way would be to use LocalBroadcastManager.
The code will be as follows.
//ACTIVITY A CODE
TextView username_tv;
String username;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
username_tv = (TextView) findViewById(R.id.username_tv);
//Get your username and store it in username
username = getYourUsername();
username_tv.setText(username);
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this);
lbm.registerReceiver(receiver, new IntentFilter("USER_NAME_CHANGED_ACTION"));
}
public BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (intent != null) {
username = intent.getStringExtra("username");
username_tv.setText(username);
}
}
};
//ACTIVITY C CODE
//ADD this code to your 'SAVE' Button Listener
Intent intent = new Intent("USER_NAME_CHANGED_ACTION");
intent.putExtra("username", editText.getText().toString());
// put your all data using put extra
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
For more information on localBroadcastManager look here.

I do not understand why you need this, but you can do it by saving the value of EditText in ActivityC in SparedPreferences here is an example how can make it. Then you should check in onResume() method of ActivityAif there is saved value in SparedPreferences and if has some get the value and set it to "Username" TextView.

EventBus will solve your problem
http://greenrobot.org/eventbus/

Related

How to Remove Edit text data before returning to that Activity?

Say Activity A has a Edittext, on entering some information and on clicking a button in that particular activity it goes to Activity B.
On returning to Activity A by clicking back button from B, the content in the Edit text remains as such.. help me getting it erased.
There are several ways to do that:
1) Remove EditText data before start ActivityB
2) Use startActivityForResult to launch your ActivityB and override onActivityResult in your ActivityA
3) Override ActivityA onResume method and remove EditText data inside that method. With this approach, your EditText data could be flushed even if you do not start ActivityB, so be aware of Activity lifecycle.
To do so, just do:
editText.setText("");
just before calling startActivity(intent) method, reset it by setting the editText.setText("");
before Intent set your edittext to ("");
EditText name = (EditText)findViewByid(R.id.edittext);
name.settext("Your Name");
Button send = (Button)findViewById(R.id.button);
send.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
name.settext("");
Intent activity_b = new Intent(ActivityA.this, ActivityB.class);
startActivity(activity_b)
}
});
or you can also do
#Override
public void onResume(){
super.onResume();
name.settext(");
}
editText.setText("");
you can put above line of code in two different ways
If you want to make only edittext empty when ActivityA comes back from ActivityB then just put it before the line of code where you are starting ActivityB.
edittext.settext("");
Intent intent = new Intent(ActivityA.this, ActivityB.class);
startActivity(intent)
In onResume() if you want to cover all different scenarios. It will make edittext empty whenever that activity returns whether from another activity or from the background.
Hope it will help you.

how to link a new activity in android studio using a clickable text

Please how do I link a new activity from my main activity using a clickable text. I have set the text to clickable from my main.xml but I don't know how to call the new activity from my MainActivity.java class. I know I have to use this code "textView.setOnClickListener(new View.OnClickListener());" I found in a similar question, but I don't know how and where to place it on my MainActivity.java class so that it calls a the next activity I named display
Check out Intent. You use these to start new activities or services within your application.
You're correct in that you have to assign an OnClickListener interface to your text, after you made it clickable. In the interface's onClick() method you would need to do something like this.
For example:
#Override
public void onClick(View v) {
// Create the intent which will start your new activity.
Intent newActivityIntent = new Intent(MainActivity.this, NewActivity.class);
// Pass any info you need in the next activity in your
// intent object.
newActivityIntent.putExtra("aString", "some_string_value");
newActivityIntent.putExtra("anInteger", some_integer_value);
// Start the new activity.
startActivity(newActivityIntent);
}
In the next activity, you can retrieve the intent used to start it, so that you'll have access to the data you passed from the first activity, like so:
#Override
public void onCreate(Bundle savedInstanceState) {
// Get the intent that started this activity.
Intent startingIntent = getIntent();
// Retrieve the values.
String aString = startingIntent.getStringExtra("aString");
Integer anInteger = startingIntent.getIntExtra("anInteger", 0); // 2nd param is the default value, should "anInteger" not exist in the bundle.
// Use the values to your hearts content.
}
Hope that helps.

How to put EditText info into a Intent variable

I'm trying to get the user information typed in the edit text. I want it saved into the Intent result variable. Trying to sending it back to the main activity afterwards. Keep getting the cannot resolve method. I'm thinking it must be that I'm missing a parameter in the putExtra() method
public class EnterDataActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_enterdata);
Button doneButton = (Button) findViewById(R.id.button_done);
final EditText getData = (EditText) findViewById(R.id.enter_data_here);
doneButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent result = new Intent();
result.putExtra(getData);
setResult(RESULT_OK, result);
finish(); // Ends sub-activity
}//ends onClick
});
}//ends onCreate void button
}
Well if you are trying to send the EditText that's not possible.
If you are trying to send the text that are on that EditText that's possible.
How to do it?
Declare a String to save the data (You can avoid this step, but that's more clear)
String mText = getData.getText().toString();
Then you'll use the getExtra() method to send the String to the new Activity
Intent i = new Intent(this, MyNewActivity.class);
i.putExtra("text_from_editText", mText);
startActivity(i);
Then the last step (You don't ask for it, but you'll need it), get the text.
//onCreate() of the second Activity
Intent i = getIntent();
String mText = i.getStringExtra("text_from_editText");
Replace result.putExtra(getData); with result.putExtra(getData.getText().toString()); to get the text from the EditText. Right now you're trying to put the entire EditText object into the intent as an extra instead of just the text from the EditText.

calling the same activity using a for

Hi everyone.
I'm new in android and I'm working on an app in which I need to recall the same activity to enter the information of a variable amount of entities (passengers).
I have the following:
btnContinue3.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
for (int i=0; i<Pssngr; i++){
passenger[i] = new
Intent(getApplicationContext(), Pasajeros.class);
startActivity(passenger[i]);
}
}
});
Pssngr is the amount of passsengers or entites that need a unique activity to get their information entered.
The trigger is the button then the activities will be called one by one following an array
I try this code but after clicking on the button the app crashed.
Please someone help me find a way to make this work.
thanks
It crashes because You are trying to start x number of Activities at once.
If You have to run new Activity for each of Passengers best in this scenario will be startActivityForResult
I beliver effect You trying to get is that user clicks on button just once and activities for each passenger will open one after another.
To do it in method onClick You will start only first activity, don't use loop.
You start consequently next activities in onActivitiyResult
In addition to what Gustek mentions above, a better way to approach this would be to have one activity and just pass the different values from the parent (PassengersAvitivty) activity through the intent as below:
final Intent intent = new Intent(PassengersAcitivty.this, PassengersEntityActivity.class);
intent.putExtra("PASSENGER_FIRSTNAME", passenger[i].getName());
intent.putExtra("PASSENGER_LASTNAME", passenger[i].getLastName());
intent.putExtra("PASSENGER_EMAIL", passenger[i].getEmail());
startActivity(intent);
and here is how you can retrieve them on your activity (PassengerEntityActivity)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle extras = getIntent().getExtras();
if (extras != null)
{
firstname = extras.getString("PASSENGER_FIRSTNAME");
lastname = extras.getString("PASSENGER_LASTNAME");
email = extras.getString("PASSENGER_EMAIL");
}
else
{
//Log.v("NO VALUE", "OOPS");
}
I can clarify further if needed.

Need help to save/restore a string between activities

I'm learning Android and I didn't find an answer on the web. I want to save a string from an EditText on my MainActivity to restore it on my third Activity -> Activity3
I want this string to be displayed on a TextView of this Activity.
public class MainActivity extends Activity {
private EditText nomPrenom;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nomPrenom = (EditText) findViewById(R.id.nomPrenom);
...................}
in my activity_main.xml :
android:id="#+id/nomPrenom"
android:text="#string/nomPrenom"
and
public class Activity3 extends ListActivity {
private TextView yourName;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity3);
yourName = (TextView) findViewById(R.id.nomPrenom);
yourName.setText("take the name of activity 1 here"); //<-- this line causes crash !!
..................}
here is activity3.xml
android:id="#+id/yourName"
android:text="#string/nomPrenom"
How can I do that ?
First of all your question is not so clear.. But as per given i think you need to fetch a data from edittext ( in main activity ) and then pass it to the third activity .
For This you need to use Intent to pass the data from one activity to other activity.
Example -
nomPrenom = (EditText) findViewById(R.id.nomPrenom);
nomPrenom .getText.toString();
Intent in = new Intent(MainActivity.this, Activity3 .class);
in.putExtra("value",nomPrenom .getText.toString()) ;
In Activity3 receive intent like this :-
String strValue = getIntent().getExtras().getString("value");
yourName = (TextView) findViewById(R.id.yourName);
yourName.setText(strValue);
Use Intent.putExtra("Your text here") and pass it to other activities.
or you can take the string from EditText and make the string Global, public and static and then use it in any other class.
You can do it with many ways ...
You can use shared preference so that you can access it from anywhere. But shared preference will retain your value after your application closed.
Another way pass your value with your activity intent when you are creating your activity like this: intent.putExtra' and get value into your activity viagetExtra`
Another way you can define your variable at global place consider create one class too hold such common variables and from there you can access those variable from anywhere.
All three ways can be changed according to your requirement.
In order to move from MainActivity to Activity3 write the following code
//get your edittext's text here
String text = nomPrenom.getText().toString();
//move to Activity3
Intent intent = new Intent(MainActivity.this, Activity3.class);
//pass the edit text value to Activity3
intent.putExtra("EditTextValue", text);
//start Activity3
startIntent(intent);
In Activity3, to get your edit text value do the following.
//get your edit text value here which is passed in MainActivity
String text = getIntent().getStringExtra("EditTextValue");
The variable "text" is the final output which you are looking for.

Categories

Resources