1

I'm a beginner in android studio and i want to write a code with 3 activities. 1st is for starting the app.2nd is for showing an English word and 3rd is for showing the English word and a description of it in two texts.

I want to transfer data of English word itself and its description and the number of the word to show the words one by one.

I wrote the code with help of tutorial clips but it ain't work and in 3rd activity shows nothing.

these are my codes:

main activity:

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void start(View view)
    {
        Intent i = new Intent(this,ActivityOne.class);
        startActivity(i);
    }
}

ActivityOne

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class ActivityOne extends AppCompatActivity {

    int a=0;
    String E , P;

    private Button show;
    private TextView Eword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_one);
        Eword = (TextView) findViewById(R.id.Eword);
        show = (Button) findViewById(R.id.show);

    }
    public void show(View view)
    {

        Intent intent = new Intent(this,ActivityTwo.class);
        startActivity(intent);
        int a = getIntent().getIntExtra("counter",0);
        Eword.setText(Eng[a]);
        E = Eword.getText().toString();
        P = Fa[a];
        a++;
        intent.putExtra("counter",a);
        intent.putExtra("EWord",E);
        intent.putExtra("PWord",P);

    }

    private String[] Eng = {
            "Abundance",
            "Anxiety",
            "Bruxism",
            "Discipline",
            "Drug Addiction"
    };
    private String[] Fa = {
            "Abundance Description",
            "Anxiety Description",
            "Bruxism Description",
            "Discipline Description",
            "Drug Addiction Description"
    };
}

ActivityTwo

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

public class ActivityTwo extends AppCompatActivity {
    TextView Eng , Fa;
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_two);
        Eng = (TextView) findViewById(R.id.eng);
        Fa  = (TextView) findViewById(R.id.fa);

        // recieve data from activity one
        String EWord = getIntent().getStringExtra("EWord");
        String PWord = getIntent().getStringExtra("PWord");
        int a = getIntent().getIntExtra("counter",0);

        Eng.setText(EWord);
        Fa.setText(PWord);
    }

    public void iknow(View view)
    {
        Intent myIntent = new Intent(this,ActivityOne.class);
        startActivity(myIntent);
        int a = getIntent().getIntExtra("counter",0);
        myIntent.putExtra("counter",a);
    }
    public void idknow(View view)
    {
        Intent myIntentTwo = new Intent(this,ActivityOne.class);
        startActivity(myIntentTwo);
        int a = getIntent().getIntExtra("counter",0);
        myIntentTwo.putExtra("counter",a);
    }
}

And it shows this result:

It is the third activity and it can clear the pretext that was set in design page but can not replace EWord or PWord

Can someone help me?????

Danial
  • 11
  • 1

1 Answers1

0

(1) To get you started, you need to put your variables into the intent before you start the activity. (2). I don't think its a good idea to have activity one start two, which then can start one. It is better to just close second activity which then automatically returns to one. (3) To pass data back to the activity you are returning you need to startActivityForResult https://developer.android.com/training/basics/intents/result.html

activity one starts activity two using startActivityForResult. Activity Two returns an intent (which is the result intent) and activity one recieves this in onActivityResult.

check out this: Android: how to make an activity return results to the activity which calls it?