This is kinda tricky. I have a profile Fragment and an 'Edit details' Button that opens a new Activity through startActivityForResult().
In onActivityResult() I reload the TextViews with the new data after editing the info through setText(). It works fine.
But if I rotated the Profile Fragment prior to clicking the edit button, the TextViews won't display the new data after saving them. It seems like onSaveInstanceState() saves the TextView texts and won't let onActivityResult() change them...
Call setText() inside runOnUiThread...
runOnUiThread(new Runnable() {
#Override
public void run() {
display.setText("new text");
}
});
Related
I have RecyclerView that is filled with a list of items received from the server response.
First, the user enters a log / password and selects a specific content, such as: 1 content.
After login the user go to the activity with has RecyclerView, where there are fields of content 1.
When user click on the Back button he goes back to the main activity of registration and selects the already content 2. After clicking the Login button, again goes to RecyclerView, but for some reason it has fields from the content 1 and fields content 2 Add to end of list.
I use this methods :
#Override
public void onBackPressed() {
super.onBackPressed();
recyclerView.removeAllViewsInLayout();
recyclerView.removeAllViews();
unswerFromMain.clear();
recyclerAdapter.updateAdapter(unswerFromMain);
}
This is in Adapter :
public void updateAdapter (ArrayList<Unswer> updateUnswer){
getUnswer.clear();
getUnswer.addAll(updateUnswer);
notifyDataSetChanged();
}
All this method didn`t work.
I believe this is because you are calling onBackPressed() before you do anything else. This means those other lines never run. Try swapping it around:
#Override
public void onBackPressed() {
recyclerView.removeAllViewsInLayout();
recyclerView.removeAllViews();
unswerFromMain.clear();
recyclerAdapter.updateAdapter(unswerFromMain);
super.onBackPressed();
}
Set an empty array into your adapter and notify
adapter.setItems(new ArrayList<MyPOJO>);
adapter.notifyDataSetChanged();
EDIT
You should call super.onBackpressed for the last. Like this
#Override
public void onBackPressed() {
recyclerView.removeAllViewsInLayout();
recyclerView.removeAllViews();
unswerFromMain.clear();
recyclerAdapter.updateAdapter(unswerFromMain);
super.onBackPressed();
}
I found a bug.
The problem was not to clean the adapter, it was not cleansed ArrayList which was from the main activity. This ArrayList contains a field content 1 and when I was at activity with RecyclerView and pressed the Back button that returns to the main activity already filled ArrayList data content 1. Then I chose the content 2 and the data on it just added to the end of the ArrayList.
I completely removed all methods onBackPressed (), except super.onBackPressed (); and everything was working fine. Thank you all !
I'm tried with above answer are not work for me.
So I have handle RecyclerView with hide and show.
recyclerview.setVisiblity(View.GONE);
recyclerview.setVisiblity(View.VISIBLE);
and my logic is,
first initialize one int value globally like, int clicked = 0;
BackPress event use that below code:
public void onBackPressed(){
if(recycler_view!=null){
if(clicked==0){
recycler_view.setVisibility(View.GONE);
clicked =1;
}else{
finishAffinity();
}
}else {
finishAffinity();
}
}
When you show(recyclerview.setVisiblity(View.VISIBLE);) recyclerview in activity that time set Clicked =0
So, easy way to show and hide recyclerview as well as backpress also handled.
I have button with setText() to TextView. When I click it the text changes. Next, I change activity to another and back to main activity. Now setText() doesn't work (text isn't replaced).
My code:
public void test(View view) {
String dataS = "dupa slonia";
data.setText(dataS);
System.out.println(dataS);
}
Try using onBackPressed() in the function which executes when button is clicked for going back to MainActivity.java
When you back to an Activity, you don't pass by the OnCreate method.
You have to put your code on the OnResume() method.
Take a look to the Activity Lifecycle.
I have this problem. I wrote a program and I have this code in the MainActivity class:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent activity = new Intent(MainActivity.this, AnotherActivity.class);
startActivity(activity);
output.setText(object.toString());
}
});
In AnotherActivity I modify the object and after that activity ends (with finish()) i want to "refresh" the textView of the MainActivity.
At the end of AnotherActivity the object is actually modified, but the text is not refreshed. If I click again on button, before new Activity is started (with his layout) the text is refreshed as it should, and if I close the AnotherActivity layout, the text is well refreshed. But if I don't click again that button, the text remains the old one. How can I do? Sorry for bad English or bad explanation.
You can use onResume() in the first activity to update your UI when the second activity is finished.
Either that or start the second activity with startActivityForResult which will call onActivityResult for you when the second activity is finished.
I've looked at various examples and dont quite understand exactly how this can be done.
I have 2 fragments each hosted by a seperate Activity and need to pass data between the two and update each other based on choices.
I have a clickable Textview in fragment1 that should open fragment2 and allow the user to select a choice from another list of TextViews each displaying a different option.
I want the choice of the user in fragment2 to update the text of the TextView in fragment1 based on the choice that was made.
I have a listener to start fragment 2 when the TextView is clicked:
v3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(getActivity(), FragmentActivity2.class);
getActivity().startActivity(i);
}
});
When I click a TextView in fragment2 I would like the fragment to close, return to fragment1 and update the text
The easiest way to pass data between fragments is using listeners. You can implement one/two interface to exchange data (one for each activity). Your activity should implement an interface and then call the methdod in the inteface to update data:
public interface ChangeLinkListener {
public void onLinkChange(String link);
}
In the onclick method:
(ChangeLinkListener) getActivity()).onLinkChange(data.getLink());
Where onLinkChange is simply a method for example you could change the name as you prefer.
I've written a tutorial between passing data if you like you can look here
I'm working with fragments. Suppose I have fragmentA and fragmentB and I can go from fragmentA to fragmentB. In fragmentA I have a form (several edit texts). If I fill the form and then go to fragmentB and then press back button, all the information that I typed in the form is retained. From onCreateView I'm trying to do
String name = myEditText.getText().toString();
But I get empty result, even when I'm seeing the values in the form.
The question is, how can I access the content of this EditText in this situation?.
Solution
public void onResume() {
super.onResume();
String id = etIdHidden.getText().toString();
if(id.equals("")==false){
//Here do whatever you need, in my case I needed to change a bundle
mArgumentos=new Bundle();
mArgumentos.putString("id", ""+id);
}
};
Try fetching the value from the editText in the onResume() method. Also refer to the life-cycle of the fragment in the docs http://developer.android.com/guide/components/fragments.html