I have to create two activity pages. In the first the user enters their name, which is passed to the second activity. That is all working, but when the second button is pressed "button_do" the app needs to exit, when the first "button_dont" it needs go to the previous activity to re enter the name. Also as another point I need to have the result of the editText go into a sharedPreferance as long as it is not already there... having problems with both aspects. Also the instructions say that the value of the button pressed needs to be 0 or 1.
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_row="4"
android:layout_column="3"
android:layout_weight="1"
android:layout_gravity="fill"
>
<TextView
android:id="@+id/textView"
android:layout_width="256dp"
android:layout_height="66dp"
android:layout_row="0"
android:layout_column="0"
android:text="@string/enterName"
android:textSize="30sp" />
<EditText
android:id="@+id/editText"
android:layout_width="441dp"
android:layout_height="wrap_content"
android:layout_row="1"
android:layout_column="0"
android:layout_columnSpan="2"
android:textSize="40sp"/>
<Button
android:id="@+id/next"
android:layout_width="127dp"
android:layout_height="64dp"
android:layout_row="2"
android:layout_column="0"
android:text="@string/next" />
</GridLayout>
<?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=".NameActivity"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/welcome"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/welcome"
android:textSize="30sp" />
</LinearLayout>
<LinearLayout
android:layout_width="416dp"
android:layout_height="87dp"
android:orientation="horizontal">
<Button
android:id="@+id/button_dont"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/dont_call"
android:layout_margin="5sp"/>
<Button
android:id="@+id/button_do"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/thank_you"
android:layout_margin="5sp"/>
</LinearLayout>
</LinearLayout>
package com.example.androidlabs;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
Button next;
EditText editText;
public static final int REQ_CODE=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button next = findViewById(R.id.next);
EditText editText = findViewById(R.id.editText);
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(),NameActivity.class);
String name = editText.getText().toString();
i.putExtra("Value",name);
startActivityForResult(i,REQ_CODE);
}
});
}
@Override
protected void onPause() {
Object fileName;
SharedPreferences prefs = getSharedPreferences("name.txt", Context.MODE_PRIVATE);
super.onPause();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(REQ_CODE==1){
if(resultCode == REQ_CODE){
int result = data.getIntExtra("result",0);
if(result==0){
editText.setText("");
} if(result==1){
finish();
}
}
}
}
}
package com.example.androidlabs;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class NameActivity extends AppCompatActivity {
public static final int REQ_CODE=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_name);
TextView welcome = findViewById(R.id.welcome);
Intent i = getIntent();
String name = i.getStringExtra("Value");
welcome.setText("Welcome "+ name +"!");
Button button_dont = findViewById(R.id.button_dont);
Button button_do = findViewById(R.id.button_do);
button_dont.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int result=0;
Intent resultIntent = new Intent();
resultIntent.putExtra("result",result);
setResult(REQ_CODE, resultIntent);
finishActivity(REQ_CODE);
}
});
button_do.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int result=1;
Intent resultIntent = new Intent();
resultIntent.putExtra("result",result);
setResult(REQ_CODE, resultIntent);
finish();
}
});
}
}