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;
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);
btnSimpleString.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String s = etOne.getText().toString();
if(TextUtils.isEmpty(s)){
etOne.setError("Empty");
return;
}
etOne.setError(null);
SIMPLE_STRING = s ;
Intent intent = new Intent(MainActivity.this, DataGettingActivity.class);
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);
int p = -0;
try {
p = Integer.parseInt(s);
} catch (NumberFormatException e) {
throw new RuntimeException(e);
}
if(p==-0){
Toast.makeText(MainActivity.this, "Please Write Valid Number", Toast.LENGTH_SHORT).show();
return;
}
SIMPLE_INT = p ;
Intent intent = new Intent(MainActivity.this, DataGettingActivity.class);
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"/>
<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"/>
<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);
}
}
}
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