In my MainActivity, I create a variable called "pengar". I then send this variable to my second activity using putExtra on the intent. In the second activity, I edit the variable, however as I use the back button to return to my main activity, I can't use putExtra.
Write below code in your MainActivity
protected void onCreate(Bundle savedInstancesState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bundle extras = getIntent().getExtras();
if (extras != null)
{
pengar = extras.getString("key2");
}
}
public void nextActivity(View view)
{
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("key1",pengar);
startActivity(intent);
}
Write below code in your SecondActivity
protected void onCreate(Bundle savedInstancesState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Bundle extras1 = getIntent().getExtras();
if (extras1 != null) {
var2 = extras1.getString("key1");
}
public void backButton(View view)
{
Intent intent2= new Intent(this,MainActivity.class);
intent2.putExtra("key2",var2);
startActivity(intent2);
}
}
Hope this helpful
Related
I did onClick at xml level. It was okay for a few times, but this one time causes error. I can't seem to find where the error is from. But it said that startActivity(intent); is where the error is.
public void callNextSignUpScreen2(View view) {
Intent intent = new Intent(getApplicationContext(),SignUp3.class);
startActivity(intent);
}
This is my full code
public class SignUp2 extends AppCompatActivity {
//Variable
ImageView back_icon;
Button next, login;
TextView title_text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_up2);
//Hooks
back_icon = findViewById(R.id.signup_back_btn);
next = findViewById(R.id.signup_next_next_btn);
login = findViewById(R.id.login_btn);
title_text = findViewById(R.id.signup_title_text);
}
public void callNextSignUpScreen2(View view) {
Intent intent = new Intent(getApplicationContext(),SignUp3.class);
startActivity(intent);
}
public void callBackSign2(View view) {
Intent intent = new Intent(getApplicationContext(),SignUp.class);
startActivity(intent);
}
}
I have 3 activities: A->B->C
The user starts from activity A. When going to B he can press back and return to A.
But... When user goes to C from B. I wish that A and B will be removed and user will exit the app when clicking back.
I tried:
Intent intent = new Intent(B.this, C.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
but on back it still goes to B. If doing so:
Intent intent = new Intent(VerifyPhoneActivity.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
The user goes back to A, so.. It is not what I need.
Override onBackPressed() in Activity C
#Override
public void onBackPressed() {
finishAffinity();
}
This will cause all Activitys running in the same task to finish. If the user starts the app again by tapping on it in the recent tasks window, Activity A will be shown.
100% working.
try to start Activity C like this.
Intent intent = new Intent(this,ActivityC.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity();
Start B activity with startActivityForResult():
Intent intent = new Intent(A.this, B.class);
startActivityForResult(intent, 100);
and override in class A startActivityForResult():
#Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 100) {
if (resultCode == 1) {
finish();
}
}
}
Now in B class set the result to be sent back to activity A:
Intent intent = new Intent(VerifyPhoneActivity.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
setResult(1);
finish();
If I understood you right, You could achieve this is using SharedPreferences:
In Your Activity A add this:
public class ActivityA extends AppCompatActivity {
SharedPreferences prefs;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
if (savedInstanceState == null){
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
editor.putBoolean("CloseApp", false);
editor.commit();
}
}
#Override
protected void onResume() {
super.onResume();
boolean finish = prefs.getBoolean("CloseApp", false);
if (finish){
this.finish();
}
}
In Activity B:
public class ActivityB extends AppCompatActivity {
SharedPreferences prefs;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
}
#Override
protected void onResume() {
super.onResume();
boolean finish = prefs.getBoolean("CloseApp", false);
if (finish){
this.finish();
}
}
And Activity C:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
editor.putBoolean("CloseApp", true);
editor.commit();
}
#Override
public void onBackPressed() {
super.onBackPressed();
this.finish();
}
That should work.
you can just write this in Activity C if your application work from JELLY_BEAN to up .
#RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
#Override
public void onBackPressed() {
super.onBackPressed();
finishAffinity();
}
if your app work under JELLY_BEAN you can use this solution :
Activity C :
#Override
public void onBackPressed() {
Intent i = new Intent(C.this,A.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.putExtra("BackFromC",true);
startActivity(i);
super.onBackPressed();
}
Activity A in onCreate :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_a);
if(getIntent() != null){
if(getIntent().getBooleanExtra("BackFromC",false)){
finish();
}
}
... your Code
}
I'm trying to display the name inputted by the user on a basic form I created using Explicit intent on a second activity on a TextView but when I click the button the app crashes with the following "Unfortunately, formIntent has stopped" but my code has no errors. I got this to work with a Toast message on new activity but with a TextView. Any help would be appreciated.
Here's my First Activity `public class MainActivity extends AppCompatActivity {
Button submit;
TextView name;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
submit = (Button)findViewById(R.id.btnSubmit);
name = (TextView)findViewById(R.id.editTextname);
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//from sending Activity
String userName = name.getText().toString();
String value = "Thank you " + userName + "your request is being processed" ;
Intent sendIntent = new Intent(v.getContext(), Main2Activity.class);
sendIntent.putExtra("userName", value);
//Verify that the intent will resolve to an activity
if (sendIntent.resolveActivity(getPackageManager()) != null)
{
startActivity(sendIntent);
}
}
});
}
}
`
Here's My second Activity
public class Main2Activity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Bundle data = getIntent().getExtras();
if(data == null){
return;
}
String getName = getIntent().getExtras().getString("userName");
final TextView inputMessage = (TextView)findViewById(R.id.display);
inputMessage.setText(getName);
}
}
Please change this line to your activity name
Intent sendIntent = new Intent(MainActivity.this, Main2Activity.class);
I´m new to app development and I´m trying to figure out how to send an user input like name, to a list view located in a diferent activity.
Thanks in advance.
MainActivity:
public class MainActivity extends AppCompatActivity {
private Button mbuttonNext;
private EditText meditTextName;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
meditTextName = (EditText) findViewById(R.id.editTextName);
mbuttonNext = (Button) findViewById(R.id.buttonNext);
mbuttonNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, ClientList.class);
startActivity(intent);
}
});
}
}
ListViewActivity:
public class ClientList extends AppCompatActivity {
private ListView listaClientes;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_client_list);
listaClientes = (ListView) findViewById((R.id.listView));
}
}
In the main activity you need to need intent.putExtra
mbuttonNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, ClientList.class);
intent.putExtra("VariableName", "Fred");
startActivity(intent);
}
});
Than in listView
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_client_list);
listaClientes = (ListView) findViewById((R.id.listView));
Bundle bundle = getIntent().getExtras(); //get the intent and data passed
//next check that bundle is not null
if (bundle != null) {
String name = bundle.getString("VariableName");
//try loging out the value
Log.i("Name", name);
}
}
I believe this will solve your problem
When starting the new activity:
Intent newIntent = new Intent(MainActivity.this, ClientList.class);
newIntent.putExtra("com.example.myapp.NAME", "someName");
startActivity(newIntent);
Then in ClientList:
Intent intent = getIntent();
String name = intent.getStringExtra("com.example.myapp.NAME");
Can then use name variable as source data for the ListView.
Usually you should create an Adapter that will "set" the items in the ListView. In order for you to send/transfer the user input to another activity you can use "putExtra()". You can do that the following way:
intent.putExtra("str1",item)
where str1 is the key to get your item in the activity you started. To do that you should do:
Bundle b = getIntent().getExtras();
Obj a = (TypeCast) b.get("str1");
After that you should read how to create an Adapter and set it in a ListView in order for your ListView to show the item.
There is an example code that passed data by using Intent below the three Activity codes.
Activity A
public class A extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_A);
}
public void onButton1Clicked(View v) {
Intent intent = new Intent(getApplicationContext(), B.class);
startActivity(intent);
}
Activity B
public class B extends AppCompatActivity {
TextView tv;
ImageButton ib;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_B);
tv = (TextView) findViewById(R.id.textView);
ib = (ImageButton) findViewById(R.id.imageButton);
ib.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(B.this, C.class);
startActivityForResult(intent, 2);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 2) {
if (data != null) {
String message = data.getStringExtra("DATA");
tv.setText(message);
}
}
}
public void onButton2Clicked(View v) {
Intent intent = new Intent(getApplicationContext(), C.class);
startActivity(intent);
}
Activity C
public class C extends AppCompatActivity {
EditText et;
Button btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_C);
et = (EditText) findViewById(R.id.editText);
btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String data = et.getText().toString();
Intent intent = new Intent();
intent.putExtra("DATA", data);
setResult(2, intent);
finish();
}
});
}
The order of activity is A - B - C.
In A, there is a Button that can move to B, and in B there are a Button(can move to C) and TextView. In C, a Button(can move to B again) and EditText.
For example, Using 'startActivityForResult' I have output as TextView(B) that received a text which was input by EditText in C. But after moving B into A(Back), an insertion of TextView gets disappeared when entering to B again. In addition, even if entering into C, there is no insertion of EditText, too.
I would really need and know 'Save code' into variable by inputting as EditText when pressing a button in C.
In this case, HOW can I add ‘Code’ the remain the insertion value either the insertion value remains by quitting the application or moves to Activity that received DATA like A?
Thanks for your cooperation and concern.
To maintain the state of your activity, you have to override onSaveInstanceState method. Store the value of your TextViews and EditText in this method. For example, let's talk about Activity B. In activity B you would do something like this:
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
// Save the user's current game state
String text = tv.getText().toString();
savedInstanceState.putString("mytext", text);
// Always call the superclass so it can save the view hierarchy state
super.onSaveInstanceState(savedInstanceState);
}
Then in your onCreate do this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); // Always call the superclass first
// Set Content View and initialize the views
// Check whether we're recreating a previously destroyed instance
if (savedInstanceState != null) {
// Restore value of members from saved state
String mytext = savedInstanceState.getString("mytext");
tv.setText(mytext);
} else {
// Probably initialize members with default values for a new instance
}
...
}
You can read more about Recreating an Activity.
You can store intent's data using following way..
Your example seems confusing on how you passing the data and you have two methods in ActivityB handing the Intent. (onButtonClicked and StartActivityForResult())
Here's simple example
Define unique string to save your Intent's data in
- ActivityA
public final static String EXTRA_MESSAGE ="myMessage";
below method will open the ShowMessage Activity showing the message you type in EditText view.
public void sendMessage(){
Intent intent = new Intent(this, ShowMessage.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
Here's how you can retrieve that data in ShowMessage activity.
-ActivityB.
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
//EXTRA_MESSAGE is a unique string that holds the message typed in TextView
TextView receiveMessage = findViewById(R.id.sentMessage);
receiveMessage.setText(message); //would set the message typed in ActivityA
Now when you get back and if you still want your EditText to have the previous string entered, you can do as the answer given by Eric B.
//Saving the value
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
String myMessage = editText.getText().toString();
outState.putString("myMessage", myMessage);
}
in onCreate(), you can retrieve as follows
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null){
String myMessage = savedInstanceState.getString("myMessage","");
editText.setText(myMessage);
}
Also one thing recommended is resultCode == RESULT_OK for validation of intent.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 2 && resultCode == RESULT_OK) {
if (data != null) {
String message = data.getStringExtra("DATA");
tv.setText(message);
}
}