Guys I have 2 activity when I clicked button in main starting new activity(message.java) and I'm trying to get data from my message activity
to mainActivity .In message activity, I have edittext and when clicked button go back mainactivity and show that info in textview. This code working but problem is when I clicked button in mainActivity
it's first delete textview's text and then message activity begining . This is not what I want and also , it's taking info from message activity that's OK but in mainActivity I must clicked again mainActivtiy button to show in textview. Any suggest?
This is what I want
This picture shows problem
Parent -> MainActivity
Child ->message
public class MainActivity extends AppCompatActivity {
private RelativeLayout rLayout;
private Button bt,bt2;
private TextView tv;
private String s;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rLayout=(RelativeLayout) findViewById(R.id.relativeLayout);
tv=(TextView)findViewById(R.id.textView2);
bt2=(Button)findViewById(R.id.button4);
final Intent i=new Intent(getBaseContext(),MessageActivity.class);
bt2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivityForResult(i, 1);
tv.setText(s);
}
});
}//end of onCreate
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if(resultCode == RESULT_OK){
s=data.getStringExtra("info");
}
}
}
}//end of mainActivity
here message.java
public class MessageActivity extends Activity {
private Button bt;
private TextView tv;
private EditText et;
private String s;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.message);
bt=(Button)findViewById(R.id.button);
et=(EditText)findViewById(R.id.editText);
tv=(TextView)findViewById(R.id.textView);
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent data = new Intent();
s=et.getText().toString();
data.putExtra("info", s);
setResult(RESULT_OK, data);
finish();
}
});
}
}
Im not sure if I understood your question correctly, but if you would like to show your text in MainActivity when you come back from MessageActivity, you should just be setting the text in onActivityResult() method, not in the clickListener.
So your code will look like this:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if(resultCode == RESULT_OK){
s=data.getStringExtra("info");
tv.setText(s);
}
}
}
Why don't you try to set the textView text in the activity result function. Is the better place to do it. Think than when you start a child Activity, the flow of parent activity stops. And when you finish child activity, flow goes directly to onActivityResult function. Try it. Regards.
In your MainActivity you need to set the text on your textview once you receive it. Problem is that startActivityForResult is asynchronous call. Once call to this method is gone it will start other activity but your onClick() method will not stop here. Its a normal listener that calls onActivityResult when your activity is destroyed.
public class MainActivity extends AppCompatActivity {
private RelativeLayout rLayout;
private Button bt,bt2;
private TextView tv;
private String s;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rLayout=(RelativeLayout) findViewById(R.id.relativeLayout);
tv=(TextView)findViewById(R.id.textView2);
bt2=(Button)findViewById(R.id.button4);
final Intent i=new Intent(getBaseContext(),MessageActivity.class);
bt2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivityForResult(i, 1);
}
});
}//end of onCreate
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if(resultCode == RESULT_OK){
s=data.getStringExtra("info");
tv.setText(s);
}
}
}
}
Accept and upvote if it works.
Related
In my first Activity a "+" button opens the second activity. On the second activity I want to click a button to add to a counter and pass that increment number to my first activity. Right now I am able to add to the counter from the first activity only.
MainActivity.java
public class MainActivity extends AppCompatActivity {
private Button addBtn;
int quantity = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addBtn = (Button) findViewById(R.id.addBtn);
addBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openAddStarActivity();
}
});
}
public void openAddStarActivity() {
Intent intent = new Intent(this, AddStarActivity.class);
startActivity(intent);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if(resultCode == RESULT_OK) {
TextView textView = (TextView)
findViewById(R.id.quantity_text_view);
textView.setText(data.getStringExtra("textViewText"));
}
}
}
public void increment(View view) {
if (quantity == 100) {
return;
}
quantity = quantity + 1;
displayQuantity(quantity);
}
private void displayQuantity(int numberOfStars) {
TextView quantityTextView = (TextView)
findViewById(R.id.quantity_text_view);
quantityTextView.setText("" + numberOfStars);
}
}
AddStarActivity.java
public class AddStarActivity extends AppCompatActivity {
int quantity = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_star);
}
public void increment(View view) {
if (quantity == 100) {
return;
}
quantity = quantity + 1;
displayQuantity(quantity);
}
private void displayQuantity(int numberOfStars) {
TextView quantityTextView = (TextView)
findViewById(R.id.quantity_text_view);
quantityTextView.setText("" + numberOfStars);
}
#Override
public void onBackPressed(){
TextView textView = (TextView) findViewById(R.id.quantity_text_view);
Intent i = new Intent(this,MainActivity.class);
setResult(RESULT_OK, i);
i.putExtra("textViewText", textView.getText().toString());
startActivityForResult(i,0);
}
}
I added a Textview on the second activity so that I could simply pass that textview data to the textview data field on my first activity
Make quantity public static in MainActivity and increment the same static variable in AddStarActivity. So your function will be
public void increment(View view) {
if (MainActivity.quantity == 100) {
return;
}
MainActivity.quantity++;
displayQuantity(MainActivity.quantity);
}
You can do this thing with tow way which is bellow
Broadcast Receiver
Register Broadcast in your first activity and fire action from your second activity with data like below example
In your first activity
BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (intent != null && intent.getAction() != null && intent.getAction().equals("SendCounter")) {
counter=intent.getIntExtra("value",0);
}
}
};
registerReceiver(receiver,new IntentFilter("value"));
In your second activity
Intent intent=new Intent("SendCounter");
intent.putExtra("value",counterValue);
sendBroadcast(intent);
Static Member
Define one variable in your activity and define as static so you can access that particular member as static.
In your first activity
public static int counter;
Now in your second activity, you can add value in your first activity variable like bellow
ActivityFirst.counter=30 //New value
Your MainActivity should not be calling startActivity, but instead should call startActivityForResult, when starting AddStarActivity. Then in AddStarActivity, you would call setResult when you are incrementing. And finally, remove onBackPressed from AddStarActivity. When the user clicks the back button to go from your AddStarActivity back to MainActivity (meaning the system will finish the AddStarActivity), then MainActivity#onActivityResult will be invoked.
i have two activities A and B i am using a button to be directed to activity B i managed to write a code so it code be turned back to activity A automatically without any buttons after 5 seconds
but now i want when it return back to return data to A how it can be done
i wrote the code in A to recieve data but don't know what to add in activity B
Activity A ::
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button two = (Button) findViewById(R.id.button2);
Recieve = (TextView) findViewById(R.id.textView4);
two.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Intent Intent = new Intent(MainActivity.this, Gps.class);
//startActivity( Intent);
sendMessage();
}
});
}
public void sendMessage() {
Intent intent = new Intent(MainActivity.this, Hello.class);
startActivityForResult(intent, REQUEST_CODE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
Response = data.getStringExtra("key");
Recieve.setText("msg is " + Response);
}
}
Activity B::
public class Hello extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hello);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
final Intent i = new Intent(Hello.this, MainActivity.class);
Hello.this.startActivity(i);
Hello.this.finish();
}
}, 5000);
}
}
In my app I create two activities and I want to get input from the second activity and use it in the first activity, I use startActivityForResult but there is a problem in it!
That is the code of first activity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
final Button b=(Button)findViewById(R.id.button);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent("net.naif.action.GETDATA");
startActivityForResult(i,77);
}
});
}
protected void onActivityForResult (int requestCode,int resultCode,Intent data){
if (requestCode ==77 && resultCode ==RESULT_OK){
String msg =data.getStringExtra("text");
Toast.makeText(getBaseContext(),msg,Toast.LENGTH_SHORT).show();
}
}
And this for the second:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
final Button b=(Button)findViewById(R.id.button);
final EditText t=(EditText)findViewById(R.id.editText);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String s=t.getText().toString();
Intent i=new Intent();
i.putExtra("text",s);
setResult(Activity.RESULT_OK,i);
finish();
}
});
}
Android studio notify me that is the method ( protected void onActivityForResult (int requestCode, int resultCode, Intent data) is never used.
That's because the method is named onActivityResult, not onActivityForResult.
I've this code:
public class MainActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intent = new Intent(this, ConnectDialog.class);
update();
}
private void update(){
if(a)
startActivity(intent);
else{
//code
}
}
}
And this:
public class ConnectDialog extends Activity{
private Button btn;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_connect_dialog);
btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
finish();
}
});
}
}
The problem is: when I click on the button of the Intent, is it possible to execute the method update of the main activity again? Thank you very much
Just put the call to update into onResume() of the MainActivity. This way, it will be called at first startup and when the MainActivity is shown again later on:
#Override
protected void onResume(){
super.onResume;
update();
}
I would use the onActivityResult method.
Change your update method to
int YOUR_RESULT = 100;
private void update(){
if(a)
startActivityForResult(intent, YOUR_RESULT);
else{
//code
}
}
then in that same activity, use this method:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == YOUR_RESULT) {
update();
}
}
If you use this method, the update() method will only get called when coming back from that activity.
if u check the Android Life Cycle u will get the answer why the Update is not executing..
How ever to call the update method on click..
private void update(){
if(a)
startActivityForResult(intent, 0);
else{
//code
}
}
and use onActivityResult to call the update method again
#Override
protected void onActivityResult(int arg0, int arg1, Intent arg2) {
super.onActivityResult(arg0, arg1, arg2);
update();
}
I have a Parent activity which is sending data to child activity but the child is not returning the result. I posted portion of code from both activity altogether below- Theres a few code that I didnt post for unnecessity.
public class TdeeActivity extends Activity {
public static final int CALLED_ACTIVITY = 1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tdee);
Button ok=(Button)findViewById(R.id.btnOk);
ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent bmr= new Intent(TdeeActivity.this,BMRActivity.class);
startActivityForResult(bmr,CALLED_ACTIVITY);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case CALLED_ACTIVITY:
if (resultCode == RESULT_OK) {
Toast.makeText(this, "THE RESULT-"+data.getExtras().getString("result"),
Toast.LENGTH_SHORT).show();
}
}
}
} // end class
public class BMRActivity extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bmr);
Button btnOk=(Button)findViewById(R.id.btnOk);
btnOk.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showResult();
}
});
public void showResult() {
Intent data= new Intent();
data.putExtra("result",result);
setResult(RESULT_OK, data);
finish();
}
}
}// end class
I tested you code in a dummy project and it was working fine at my end.. following is the code for both activity:
public class ParentActivity extends Activity {
private static final int CALLED_ACTIVITY = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent bmr = new Intent(ParentActivity.this, ChildActivity.class);
startActivityForResult(bmr, CALLED_ACTIVITY);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case CALLED_ACTIVITY:
if (resultCode == RESULT_OK) {
Toast.makeText(this, "THE RESULT-"+data.getExtras().getString("result"),
Toast.LENGTH_SHORT).show();
}
}
}
}
public class ChildActivity extends Activity {
final Activity activity = this;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Returning result from child activity
Intent data = new Intent();
data.putExtra("result", "from child"
+ this.getCallingActivity().getClassName());
setResult(RESULT_OK, data);
finish();
}
}
So please post more code so that issue can be found..