part 16 - Data Passing to Next Activity By Bundle - Method 5 and 6

java part One : 

package first.learn.datapassingtwo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

public static String SIMPLE_STRING = null;
public static int SIMPLE_INT = -0;

EditText etOne, etTwo ,etThree,
etFour;
Button btnSimpleInt,
btnSimpleString;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

etOne = (EditText) findViewById(R.id.etOne);
etTwo = (EditText) findViewById(R.id.etTwo);

btnSimpleInt = (Button)findViewById(R.id.btnSimpleInt);
btnSimpleString = (Button)findViewById(R.id.btnSimpleString);

etThree = (EditText)findViewById(R.id.etThree);
etFour = (EditText)findViewById(R.id.etFour);


btnSimpleString.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

String s = etOne.getText().toString();

if(TextUtils.isEmpty(s)){
etOne.setError("Empty");
return;
}



String t = etThree.getText().toString();
if(TextUtils.isEmpty(t)){
etThree.setError("Empty");
return;
}

etOne.setError(null);
etThree.setError(null);

Bundle bundle = new Bundle();
bundle.putString("bundleStringOne", s);
bundle.putString("bundleStringTwo", t);

Intent intent = new Intent(MainActivity.this, DataGettingActivity.class);
intent.putExtras(bundle);
startActivity(intent);


}
});


btnSimpleInt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

String s = etTwo.getText().toString();
if(TextUtils.isEmpty(s)){
etTwo.setError("Empty");
return;
}

etTwo.setError(null);

String t = etFour.getText().toString();

int q = -0 ;

try {
q = Integer.parseInt(t);
} catch (NumberFormatException e) {
throw new RuntimeException(e);
}


int p = -0;

try {
p = Integer.parseInt(s);
} catch (NumberFormatException e) {
throw new RuntimeException(e);
}

if(p==-0||q==-0){
Toast.makeText(MainActivity.this, "Please Write Valid Number", Toast.LENGTH_SHORT).show();
return;
}

Intent intent = new Intent(MainActivity.this, DataGettingActivity.class);

Bundle bundle = new Bundle();
bundle.putInt("SimpleIntOne", p);
bundle.putInt("SimpleIntTwo", q);
intent.putExtras(bundle);
startActivity(intent);


}
});

}





}

 

xml part One : 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
android:layout_margin="16dp">


<EditText
android:hint="Simple String....."
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/etOne"/>

<EditText
android:hint="Simple String....."
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/etThree"/>


<Button
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btnSimpleString"
android:text="Button Simple Static String"/>


<EditText
android:hint="Simple int ....."
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/etTwo"
android:inputType="number"/>


<EditText
android:hint="Simple int ....."
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/etFour"
android:inputType="number"/>


<Button
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btnSimpleInt"
android:text="Button Simple Static Integer"/>









</LinearLayout>


java part two : 

package first.learn.datapassingtwo;

import static first.learn.datapassingtwo.MainActivity.SIMPLE_INT;
import static first.learn.datapassingtwo.MainActivity.SIMPLE_STRING;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class DataGettingActivity extends AppCompatActivity {


TextView tvShow;

private String private_simple_string = null;
private int private_int = -0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_data_getting);

tvShow = (TextView) findViewById(R.id.tvShow);

if(getIntent()!=null){

if(getIntent().getStringExtra("SimpleString")!=null){

String s = getIntent().getStringExtra("SimpleString");

tvShow.setText(s);

}

}



if(getIntent()!=null){

if(getIntent().getIntExtra("SimpleInt", -0)!=-0){

int p = getIntent().getIntExtra("SimpleInt", -25);
tvShow.setText(""+p);

}

}


if(SIMPLE_STRING!=null){

tvShow.setText(SIMPLE_STRING);
private_simple_string = SIMPLE_STRING;
System.out.println("Taken STATIC String first : " + SIMPLE_STRING);


SIMPLE_STRING = null;

tvShow.setText(private_simple_string );

System.out.println("Taken Private String : " + private_simple_string);
System.out.println("Taken Static String : " + SIMPLE_STRING);


Toast.makeText(this, "Found : "+private_simple_string, Toast.LENGTH_SHORT).show();


}


if(SIMPLE_INT!=-0){

private_int = SIMPLE_INT ;
System.out.println("Static Int First : "+ SIMPLE_INT);

SIMPLE_INT = -0 ;
tvShow.setText(""+private_int);
System.out.println("Static Int Second : "+ SIMPLE_INT);
System.out.println(" private Int : "+ private_int);

}


if(getIntent()!=null){

if(getIntent().getExtras()!=null){

Bundle bundle = getIntent().getExtras();

if(bundle!=null) {
String a = bundle.getString("bundleStringOne");
String b = bundle.getString("bundleStringTwo");
System.out.println("a : "+ a +" b : "+ b );

tvShow.setText(a + " -- "+b );

}
}
}

if(getIntent()!=null){

if(getIntent().getExtras()!=null){

Bundle bundle = getIntent().getExtras();

if(bundle!=null) {

int a = bundle.getInt("SimpleIntOne");
int b = bundle.getInt("SimpleIntTwo");

System.out.println("a : "+ a +" b : "+ b );

tvShow.setText(a + " -- "+b );

}
}
}




}



}


xml part two : 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DataGettingActivity"
android:orientation="vertical"
android:layout_margin="16dp">


<TextView
android:hint="Data....."
android:textSize="30dp"
android:textColor="@color/purple_700"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tvShow"
/>




</LinearLayout>


















No comments:

Post a Comment