0

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);
    }
}
  • There are a number of SO questions addresseding this: https://www.google.com/search?q=android+data+from+one+activity+to+another&oq=android+data+from+one+ac&aqs=chrome.1.69i57j0l5.7140j0j7&sourceid=chrome&ie=UTF-8 – Al Lelopath Mar 24 '17 at 20:18
  • 1
    Possible duplicate of [How do I pass data between Activities in Android application?](http://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android-application) – Al Lelopath Mar 24 '17 at 20:18

0 Answers0