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.
Related
I've added my acivity class in this link
Click MeI am fairly new to Android and am having difficulty trying to add a button on my second activity. I am able to place a button in my main activity and then I use it to navigate to my secondary activity (using setContentView(R.layout.)) and then I use the same 'onClick' method or even 'OnClickListener' method but the button on my second activity just wont work on another activity. Maybe i am missing something
]3
just try to do this:
public class FirstActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_activity);
findViewById(R.id.about_us).setOnClickListener(new
View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(FirstActivity.this, SecondActivity.class));
}
});
}
}
and in second activity again find your button in second activity xml by id and write onClickListener for it
You need to implement two separate methods for two different buttons. I would suggest do these things in the Java code instead of XML.
You can do some thing like this:
Button button = findViewById(R.something.something);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//perform your operation(s) here.
}
});
As I understand you try to use one layout.xml for both activities.
You need to declare method click1 in both activities, not only in first.
It means that your first activity has to have method public void click1() and the second activity has to duplicate method public void click1()
I know it's old however,
#Meikiem idea is great. When you use setContentView(View View) you are just setting the activity's
content to another view (xml), and thus not really using the other .java file which has another
onClick method defined for the second button.
Activity's setContentView(view)
You need to create an Intent and pass it along the startActivity method.
Intent Definition
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 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.
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");
}
});
I am developing an android app which has 3 tabs, created in MainActivity.java. Every tab has its own activity. In those activities I have a method called "Refresh()" to update the listview in that tab.
When the user clicks on a button the method "refreshTab(View v)" is called.
// Tab refreshen
public void refreshTab (View v) {
Activity MyActivity = this.getCurrentActivity();
MyActivity.Refresh();
}
This is throwing "The Method Refresh() is undefined for the type Activity. However, "MyActivity" is filled with the tab activity.
How would I go about getting this to work?
You need to cast the activity to your type of activity. Right now you are trying to call the Android class activity, which does not have a "Refresh" function.
Your button handler is a little over-complicated (even though it's only two lines)...
Just do something like:
// Tab refreshen
public void refreshTab (View v) {
Refresh();
}
If the way you've defined your OnClickListener is directly inline (but still within your activity's class), you may need to add a little direction, where MyClassType is the name of your class that extends Activity:
// Tab refreshen
public void refreshTab (View v) {
MyClassType.this.Refresh();
}