Transferring data from the third activity to the first - java

From the first activity I would like to go to the second, then from the second to the third. In the third activity I would like to enter the name in EditText, then after pressing the button, go to the first activity and at the same time send the information entered in the third activity.
Unfortunately, after pressing the button in the third activity, instead of returning to the first activity, I return to the second activity. Was the first activity killed? What could I do to ensure that the information is correct for the first activity? This is my code:
First:
public class MainActivity extends AppCompatActivity {
TextView textViewInformation;
Button button_GoToSecond;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textViewInformation = findViewById(R.id.textView);
button_GoToSecond = findViewById(R.id.button);
button_GoToSecond.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this, Second.class);
startActivity(i);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent i) {
if((requestCode == 1) &&(resultCode == RESULT_OK)) {
String name = i.getStringExtra("name");
textViewInformation.setText(name);
}
}
}
Second:
public class Second extends AppCompatActivity {
Button button_GoToThird;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
button_GoToThird = findViewById(R.id.button2);
button_GoToThird.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(Second.this, Third.class);
startActivity(i);
}
});
}
}
Third:
public class Third extends AppCompatActivity {
EditText editText_Data;
Button button_SendData;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
editText_Data = findViewById(R.id.editText);
button_SendData = findViewById(R.id.button3);
button_SendData.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
finish();
}
});
}
public void finish() {
String name;
name = editText_Data.getText().toString();
Intent i = new Intent(Third.this, MainActivity.class);
i.putExtra("name", name);
setResult(RESULT_OK, i);
super.finish();
}
}

just remove finish(); thats it

The reason that it goes to the second activity is because of this:
button_SendData.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
finish();
}
});
public void finish() {
String name;
name = editText_Data.getText().toString();
Intent i = new Intent(Third.this, MainActivity.class);
i.putExtra("name", name);
setResult(RESULT_OK, i);
super.finish(); //This kills the current activity.
}
You should do:
public void finish() {
String name;
name = editText_Data.getText().toString();
Intent i = new Intent(Third.this, MainActivity.class);
Bundle bundle = new Bundle();
bundle.putString("name", name);
startActivityForResult(i, RESULT_OK, bundle);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent i) {
if((requestCode == 1) &&(resultCode == RESULT_OK)) {
Bundle bundle = i.getExtras();
String name = bundle.getString("name");
textViewInformation.setText(name);
}
}
When you call finish, it just kills the current activity. If you want to go back to the first activity, just start a new activity for the first activity and pass the data in a Bundle.
If you want to have more of a stack concept, you can use Fragments.

Related

Why is the bundle always null in spite of key being present?

The code below is intended to pass the data, using bundle, collected on signing up in Signup.java to ViewProfile.java which displays the data. On checking for a key in the bundle, it returns true, however, the bundle is null when checked in ViewProfile.java. Help will b appreciated.
Signup.java
public class Signup extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup);
final EditText n=(EditText)findViewById(R.id.editText3);
final EditText u=(EditText)findViewById(R.id.editText4);
final EditText p=(EditText)findViewById(R.id.editText5);
final EditText c=(EditText)findViewById(R.id.editText6);
Button s=(Button)findViewById(R.id.button4);
final Userdatabase udb=new Userdatabase(this);
s.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
String name=n.getText().toString();
String email=u.getText().toString();
String password=p.getText().toString();
String phone=c.getText().toString();
boolean b=udb.insertuser(name,email,password);
if(b==true) {
Intent i = new Intent(Signup.this, MainActivity.class);
Bundle bundle=new Bundle();
bundle.putString("NAME",name);
bundle.putString("ID",email);
bundle.putString("PHONE",phone);
i.putExtras(bundle);
startActivity(i);
}
else
Toast.makeText(getApplicationContext(),"Please try again",Toast.LENGTH_SHORT).show();
}
});
}
}
ViewProfile.java
public class ViewProfile extends AppCompatActivity {
String name,username,contact,profession;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_profile);
Intent intent=getIntent();
Bundle b=intent.getExtras();
if(b!=null) {
name = b.getString("NAME");
username = b.getString("ID");
contact = b.getString("PHONE");
TextView tv1=(TextView)findViewById(R.id.textView17);
TextView tv2=(TextView)findViewById(R.id.textView18);
TextView tv3=(TextView)findViewById(R.id.textView19);
tv1.setText(name);
tv2.setText(username);
tv3.setText(contact);
}
ImageView img=(ImageView)findViewById(R.id.imageView4);
img.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Intent i=new Intent(ViewProfile.this,Profile.class);
startActivity(i);
}
});
}
}
You are calling the wrong activity in the SignUp activity:
if(b==true) {
Intent i = new Intent(Signup.this, MainActivity.class);//problem here
You have set the intent as MainActivity.class and sending data to MainActivity and not to ViewProfile activity.
Change to this in SignUp activity:
if(b==true) {
Intent i = new Intent(Signup.this, ViewProfile.class);

int/string value becomes "0"/null when passed from one activity to another

I have two activities, MainActivity and SecondActivity. While passing int value from MainActivity to SecondActivity it becomes "0". I have tried with and without bundle, tried various solution already present on StackOverFlow, but no go.
Here is my code:
MainActivity
final Intent intent = new Intent(MainActivity.this, SecondActivity.class);
final TextView textView = findViewById(R.id.textView);
textView.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Bundle bundle = new Bundle();
bundle.putInt("category",9);
intent.putExtras(bundle);
startActivity(intent);
}
});
SecondActivity:
package com.example.app;
import android.content.Intent;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class SecondActivity extends AppCompatActivity {
ViewPager viewPager;
TabsPagerAdapter pagerAdapter;
private int mMedCategory = 6;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
// String stringCategory = getIntent().getStringExtra("category_string");
int medCategory = getIntent().getIntExtra("category_int", -1);
setMedCategory(medCategory);
viewPager = findViewById(R.id.viewpager);
pagerAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(pagerAdapter);
}
public int getMedCategory(){
return mMedCategory; //This value goes to TabsPagerAdapter
}
public void setMedCategory(int i){
mMedCategory = i;
}
}
You can try this:
MainActivity
TextView textView = findViewById(R.id.textView);
textView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("category_int", 9);
intent.putExtra("category_string", "9");
startActivity(intent);
}
});
SecondActivity
private int medCategoryInt;
private String medCategoryString;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
medCategoryInt = getIntent().getIntExtra("category_int", 0);
medCategoryString = getIntent().getStringExtra("category_string");
}
You can have a look at How do I pass data between Activities in Android application?
Use Intent#getIntExtra() to retrieve an integer value from the intent:
From MainActivity:
Intent myIntent = new Intent(MainActivity.this, SecondActivity.class);
myIntent.putExtra("category", 9);
MainActivity.this.startActivity(myIntent);
From SecondActivity:
#Override
protected void onCreate(Bundle savedInstanceState) {
Intent intent = getIntent();
medCategory = intent.getIntExtra("category", -1);
}
Note that this would assign a default value of -1 to medCategory in the event that the key cannot be found.
I'm not sure but You may use this for second activity:
SecondActivity secondActivity = new SecondActivity();
if (secondActivity.getIntent.getExtras() != null)
{
int medCat = secondActivity.getIntent.getExtras().getInt("category");
}
Use this code. Remove final and try to initialize intent in onclick method not outside of it.
In your OnClick
Intent intent=new Intent(this,Main2Activity.class);
Bundle bundle = new Bundle();
bundle.putInt("category",9);
intent.putExtras(bundle);
startActivity(intent);
In your second activity
public class Main2Activity extends AppCompatActivity {
private int mMedCategory =0;
public int getmMedCategory() {
return mMedCategory;
}
public void setmMedCategory(int mMedCategory) {
this.mMedCategory = mMedCategory;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Bundle bundle=getIntent().getExtras();
if(bundle!=null){
int demoCategory =bundle.getInt("category");
setmMedCategory(demoCategory);
}else {
Toast.makeText(this, "null", Toast.LENGTH_SHORT).show();
}
}
public void Show(View view) {
Toast.makeText(this, String.valueOf(getmMedCategory()), Toast.LENGTH_SHORT).show();
}
}
EDIT
Created a demo project for you it is working. Show method is onclick method of a button.

How to have multiple intents on the same java program

public class FullscreenActivity extends AppCompatActivity {
private ImageButton act;
private ImageButton sat;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen);
act = (ImageButton) findViewById(R.id.act);
sat = (ImageButton) findViewById(R.id.sat);
sat.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick (View v) {
Intent intent = new Intent(FullscreenActivity.this, sat.class);
startActivity(intent);
act.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick (View v) {
Intent intent = new Intent(FullscreenActivity.this, act.class);
startActivity(intent);
}
});
}}
What am I doing worng. I have a main UI with six imagebuttons, each one linking to a different activity. How can I link then all in the main activity which is called fullscreen activity
I think you want to bring the FullScreenActivity to front so you only have to finish the front Activity because you didn't finish it
just do finish(); on your sat.class or act.class
or....
You can do this too on your sat.class or act.class:
Intent i = new Intent(this, FullScreenActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(i);
act.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick (View v) {
Intent intent = new Intent(FullscreenActivity.this, act.class);
startActivity(intent);
}
});
why is the above part inside some other button's click listener? Move it outside the anonymous inner method like
sat.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick (View v) {
Intent intent = new Intent(FullscreenActivity.this, sat.class);
startActivity(intent);
}
});
act.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick (View v) {
Intent intent = new Intent(FullscreenActivity.this, act.class);
startActivity(intent);
}
});

How to start needed Activity, when I saved data in file?

When app starts at first, start Activity with two button: Create File and Settings. Then if I click on button Create File - start Activity where I write text and save in file.
If we have error when file saved - start activity with two button. Else start another Activity with three buttons: Look file, Edit File, Settings.
How organize correct transitions between this Activity and How start activity with three buttons if file already saved?
public class MainFirstActivity extends AppCompatActivity {
private Button createFile;
private Button settings;
private boolean start = true;
private static final String MY_SETTINGS = "my_settings";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_first);
createFile = (Button) findViewById(R.id.create_file);
settings = (Button) findViewById(R.id.settings);
createFile.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainFirstActivity.this, CreateFileActivity.class);
startActivity(intent);
// MainFirstActivity.this.finish();
}
});
settings.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainFirstActivity.this, SettingActivity.class);
startActivity(intent);
}
});
}
#Override
protected void onResume() {
super.onResume();
File f = new File(CreateFileActivity.FILE_NAME);
if(!f.exists() && !f.isDirectory() && start) {
Intent intent = new Intent(MainFirstActivity.this, MainWithFileActivity.class);
startActivity(intent);
//start=false;
MainFirstActivity.this.finish();
Log.e("err", "intent");
}
}
}
MainWithFileActivity
public class MainWithFileActivity extends AppCompatActivity {
public static final String MY_SETTINGS = "MY_SETTINGS";
private Button lookFile;
private Button editFile;
private Button settings;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_if_first);
lookFile = (Button) findViewById(R.id.look_file);
settings = (Button) findViewById(R.id.settings);
editFile = (Button) findViewById(R.id.edit_file);
lookFile.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainWithFileActivity.this, LookFileActivity.class);
startActivity(intent);
}
});
editFile.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainWithFileActivity.this,CreateFileActivity.class);
startActivity(intent);
}
});
settings.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainWithFileActivity.this, SettingActivity.class);
startActivity(intent);
}
});
}
}
Simply check if the file exists. If it does not, hide the lookFile button
Add this in your MainWithFileActivity onCreate method
File f = new File(CreateFileActivity.FILE_NAME);
if(!f.exists() && !f.isDirectory()) {
lookFile.setVisibility(View.GONE);
}

Using Conditions to Pass Data between Activities

Successfully I tried to pass data between three Activities.
That is :
(Data3)Activity1(Data1)-->(Data1)Activity2(Data2)-->(Data2)Activity3)
Now the Problem is:
I want pass to data between these activities using conditions. That
is in Activity2, before sending data to Activity3 I want to check
WORD = "word building"
DROP = "word built"
if WORD FROM EDITTEXT == WORD
pass data to Activity1 AND
goto Activity1
else
if WORD FROM EDITTEXT == DROP
pass data to Activity3 AND
goto Activity3
Here is the code for Activity1 named PickCard.java
public class PickCard extends Activity {
String card = "Card Picked";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
displayIntentData();
findViewById(R.id.sendButton).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(PickCard.this, BuildWord.class);
intent.putExtra("key", card);
startActivity(intent);
}
});
}
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);//must store the new intent unless getIntent() will return the old one
displayIntentData();
}
private void displayIntentData() {
Intent intent = getIntent();
TextView tv = (TextView) findViewById(R.id.intentData);
Bundle extras = intent.getExtras();
if (extras != null) {
tv.setText("Data received: " + extras.getString("key"));
} else {
tv.setText("No extradata received");
}
}
}
Here is the code for Activity2 BuildWord.java
public class BuildWord extends Activity {
String word = "Word Building";
String finished = "Word Built";
EditText simulate = (EditText) findViewById(R.id.dataToSend);
String getdata = simulate.getText().toString();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.buildword);
displayIntentData();
if (getdata == word) {
findViewById(R.id.sendButton1).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(BuildWord.this, MainActivity.class);
intent.putExtra("key", getdata);
startActivity(intent);
}
});
} else {
findViewById(R.id.sendButton1).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(BuildWord.this, DropCard.class);
intent.putExtra("key", finished);
startActivity(intent);
}
});
}
}
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
displayIntentData();
}
private void displayIntentData() {
Intent intent = getIntent();
TextView tv = (TextView) findViewById(R.id.intentData1);
Bundle extras = intent.getExtras();
if (extras != null) {
tv.setText("Data received: " + extras.getString("key"));
} else {
tv.setText("No extradata received");
}
}
}
Here is the code for Activity3 named DropCard.java
public class DropCard extends Activity {
String drop = "Card Dropped";
String declared = "User Declared";
boolean won = false;
/*Intent intent = getIntent();
TextView tv = (TextView)findViewById(R.id.intentData2);
Bundle extras=intent.getExtras();*/
String get = "word building";//extras.getString("key");
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dropcard);
displayIntentData();
if (get == "word built") {
findViewById(R.id.sendButton2).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(DropCard.this, Declare.class);
intent.putExtra("key", declared);
startActivity(intent);
}
});
} else {
findViewById(R.id.sendButton2).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(DropCard.this, MainActivity.class);
intent.putExtra("key", drop);
startActivity(intent);
//notice we dont call finish() here
}
});
}
}
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);//must store the new intent unless getIntent() will return the old one
displayIntentData();
}
private void displayIntentData() {
Intent intent = getIntent();
TextView tv = (TextView) findViewById(R.id.intentData2);
Bundle extras = intent.getExtras();
if (extras != null) {
tv.setText("Data received: " + extras.getString("key"));
} else {
tv.setText("No extradata received");
}
}
}
use
if (getdata.equals(word)){
}
instead of
if (getdata == word){
}
for comparing Strings.as in your current code replace all == with equals
if (getData.getText().toString().equalsIgnoreCase("String name"))//change this line in your code for checking
{
findViewById(R.id.sendButton1).setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent intent = new Intent(BuildWord.this,MainActivity.class);
intent.putExtra("key", getdata);
startActivity(intent);
}
});
}
else
{
findViewById(R.id.sendButton1).setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent intent = new Intent(BuildWord.this,DropCard.class);
intent.putExtra("key", finished);
startActivity(intent);
}
});
}
Its a simple you have to compare in that default method equalsIgnoreCase().
String name = edit_text.getText().toString();
if(name.equalsIgnoreCase("WellCome"))
{
//CALL YOUR ACTIVITY ONE
}
else
{
//CALL YOUR ACTIVITY THREE
}

Categories