Clear previous responses in main activity - android

I have a simple form in the main activity. On submitting the form, activity 2 starts. When I go back to the main activity (by clicking on the back button), I want to clear all the responses.

When you assign an id to any widget, and the activity or fragment stop, then the onSaveState is called for any widget to save the actual value and show it when you come back to that activity.
So, to clean up your EditText when you came back from another activity you'll have to do it manually, I suggest you to do it in onRestart function according to the Activity life cycle

You could overwrite onResume() in your main activity and clear the corresponding EditTexts there like
#Override
public void onResume() {
editText.setText("");
}
or
You can overwrite onBackPressed() in activity 2 like this
#Override
public void onBackPressed() {
Intent intent = new Intent(Activity2.this, MainActivity.class);
startActivity(intent);
}

I don't know an elegant solution, but it should works:
public class MainActivity extends AppCompatActivity {
private boolean secondActivityLaunched;
#Override
protected void onCreate(Bundle savedInstanceState) {
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
secondActivityLaunched = true;
startActivity(new Intent(MainActivity.this, SecondActivity.class));
}
});
}
#Override
protected void onStart() {
super.onStart();
if (secondActivityLaunched) {
secondActivityLaunched = false;
clearForm();
}
}
}

1st Activity: MainActivity.java
package com.test.activitytest;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText name=(EditText) findViewById(R.id.name);
EditText email=(EditText) findViewById(R.id.email);
EditText phone=(EditText) findViewById(R.id.phone);
Button submit = (Button)findViewById(R.id.button);
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent it=new Intent(MainActivity.this,Activity2.class);
it.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(it);
finish();
}
});
}
}
2nd Activity: Activity2.java
package com.test.activitytest;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class Activity2 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
}
#Override
public void onBackPressed() {
super.onBackPressed();
Intent it=new Intent(Activity2.this,MainActivity.class);
it.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(it);
finish();
}
}

Related

My code won't recognise my class when navigating to a second screen

I'm really stuck here. I'm trying to navigate to a second activity using a button but whenever i try to parse in the name of the class to the Intent method, Android Studio throws an error.
In the Intent method toWeightsScreen, it won't let me parse in a class.
Can anyone tell me what I'm messing up please.android studio snapshot
package leith.comstephen.facebook.httpswww.fitnessapp5;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button navToWeightsScreen = (Button)findViewById(R.id.Firstweights);
navToWeightsScreen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent toWeightsScreen = new Intent(this, cut.class)
startActivity(toWeightsScreen);
}
});
}
}
navToWeightsScreen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent toWeightsScreen = new Intent(getActivity(), cut.class);
startActivity(toWeightsScreen);
}
});
The this in your intent declaration refers to the button's setOnClickListener.
You should specify the actual activity. See the code below
navToWeightsScreen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Changed cut.class to Cut.class to follow coding conventions
Intent toWeightsScreen = new Intent(MainActivity.this, Cut.class);
startActivity(toWeightsScreen);
}
});

How can I call OnDestroy() when I press BackButton while I'm on First Activity?

There are two activities in my app. I want to perform a simple function. In Second Activity, If button is clicked it should hide and go back to First Activity And If I click the button in First Activity the Second Activity should be open with hidden button. I've achieved this by below code.
But the problem is. I can't close (call onDestroy())the app when I press back button while I'm in First Activity. The back button performs switch between two activities.
First Activity Java:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class first extends AppCompatActivity {
Button btn1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
btn1 = (Button) findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(first.this, second.class);
startActivity(intent);
}
});
}
public void onBackPressed() {
super.onBackPressed();
}
}
Second Activity Java:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class second extends AppCompatActivity {
Button btn2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
onBackPressed();
btn2 = (Button) findViewById(R.id.btn2);
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
btn2.setVisibility(View.GONE);
Intent intent = new Intent(second.this, first.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
}
});
}
public void onBackPressed() {
intent = new Intent(second.this, first.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
}
}
How can I call OnDestroy() in this Condition?
The reason is you override onBackPressed() in the second activity. So that means everytime you press the back button on your second activity, it will create a new first activity instead return to the old one. The app will stuck in this loop. Try to remove onBackPressed method on second activity or call finish() in it.
Call finish() from the back pressed event. Override the backpressed event so you can customise it.
#Override
public void onBackPressed() {
finish();
//super.onBackPressed();
}
}
You can use this onBackPress() method like this in first activity
#Override
public void onBackPressed() {
//super.onBackPressed();
finish();
}
OR
you can just delete onBackPress() because you don't have to override it.

Android Switch between activities

what i'm trying to do is a home screen that stays for 5 seconds and goes to activity1.When i click a button in activity1 leads me to activity2.I've tried many times to click the button but no switching happens.
homescreen (5 seconds)=Main_Activity
Activity1=selectpets.java
Activity2=fishtank.java
onclick listener seems the problem i don't know what's wrong with it
Main Activity Code
package com.set.petshome;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
Button fButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Delay Code after 5 seconds
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
setContentView(R.layout.selectscreen); //where <next> is you target activity :)
}
}, 5000);
}
//Delay End
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Now the Selectpets Code
package com.set.petshome;
import android.app.Activity;
import android.content.*;
import android.os.Bundle;
import android.view.*;
import android.widget.Button;
public class SelectPetsScreen extends Activity {
Button fButton;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.selectscreen);
//Button Fishtank Listener Start
fButton = (Button) findViewById(R.id.button1);
//Listening to button event
fButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
//Starting a new Intent
Intent nextScreen = new Intent(getApplicationContext(), fishtank.class);
startActivity(nextScreen);
}
});
//Button Fishtank Listener End
}
}
Fishtank class code
package com.set.petshome;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class fishtank extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ftank);
}
}
by the way no errors in the application just no switching after clicking
thank you very much
Here you aren't ever switching to the next Activity, just changing the layout of the current Activity
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
setContentView(R.layout.selectscreen); //where <next> is you target activity :)
}
}, 5000);
instead of setContentView() you need to use an Intent
Intent i = new Intent(MainActivity.this, SelectPetsScreen.this);
startActivity(i);
Since you aren't actually going to the next Activity (java file) your onClick() isn't set.
Edit
This is what you are doing
public class MainActivity extends Activity {
Button fButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Delay Code after 5 seconds
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
setContentView(R.layout.selectscreen); //where <next> is you target activity :)
}
}, 5000);
}
This is what you should be doing. Notice the difference in the run() function
public class MainActivity extends Activity {
Button fButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Delay Code after 5 seconds
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent i = new Intent(MainActivity.this, SelectPetsScreen.this);
startActivity(i);
}
}, 5000);
}
You can use finish() if you want use firts Activity only once.
mHandler.postDelayed(new Runnable() {
#Override
public void run() {
startActivity(new Intent().setClass(MainActivity.this, SelectPetsScreen .class).setData(getIntent().getData()));
finish();
}
}, 5000);
Be sure that you have 2nd Activity defined in your Manifest.xml:
<activity android:name="x.x.SelectPetsScreen"
android:theme="#style/NoTitle"
android:screenOrientation="nosensor"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
Avoid doing this.
setContentView(R.layout.activity_main);
//Delay Code after 5 seconds
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
setContentView(R.layout.selectscreen); //where <next> is you target activity :)
}
}, 5000);
Google gives you the Intent mechanism for switching Activities
i.e. use
startActivity(new Intent(this, yourSecondActivity.class));
instead of
setContentView(R.layout.selectscreen);
The remaining part of your code must work fine.
I was able to solve it with the help of Maxim Shoustin and everyone by:
adding second Activity to Manifest.xml which is SelectPetsScreen
Thank you so much

Displaying Another Screen Onclick in Android

package com.example.example;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
Button btn;
#Override
public void onCreate(Bundle savedInstanceState) {
this.setContentView(R.layout.activity_main);
this.btn = (Button)this.findViewById(R.id.button);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, profile.class);
startActivity(intent);
}
});
}
}
I don't get any errors however my program also dont run as I want. I am new in android and I want to change the screen after the button is clicked for that I am using two classes so in one class my program should invoke another one onclick. How can I do this ? My code is as above.
First remove this.setContentView(R.layout.activity_main); because you declared it twice . Then declare
btn = (Button)this.findViewById(R.id.button);
after
setContentView(R.layout.activity_main);
Declare your profile activity in manifest file.Check my code below.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button)this.findViewById(R.id.button);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, profile.class);
startActivity(intent);
}
});
1) your oncreate method should look like below one.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button)this.findViewById(R.id.button);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, profile.class);
startActivity(intent);
}
});
2) Add profile Activity into Manifest.
3) add logs in onClick method so you should know if it is getting called on not.
Happy coding!!

How to restore values of a activity when i move to next activity and come back again?

I want the values of edit text restored when user comes back to my first activity?
Please help me out.
Thanks in advance
this is my first activity code for getting user values in edit text
public class IntentActivity extends Activity {
EditText ed1, ed2;
float ed1_val, ed2_val;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ed1 = (EditText) findViewById(R.id.editText1);
ed2 = (EditText) findViewById(R.id.editText2);
Button next = (Button) findViewById(R.id.button1);
next.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),
Second_activity.class);
startActivity(intent);
}
});
}
/** Called when the activity is first created. */
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
ed1_val = Float.parseFloat(ed1.getText().toString());
ed2_val = Float.parseFloat(ed2.getText().toString());
Log.v("TAG", "inside saved instance");
savedInstanceState.putFloat("ed1", +ed1_val);
savedInstanceState.putFloat("ed2", +ed2_val);
}
#Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
Log.v("TAG", "inside on restore");
float ed_val = savedInstanceState.getFloat("ed1");
float ed2_val = savedInstanceState.getFloat("ed2");
ed1.setText("" + ed_val);
ed2.setText("" + ed2_val);
}
}
this is my second activity code
public class Second_activity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.second_xml);
Button back = (Button) findViewById(R.id.button1);
back.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),
IntentActivity.class);
startActivity(intent);
}
});
}
}
It is not a good idea to start the first activity again on back pressed. Call finish() in the second activity. This will lead to the resume of the first activity which is what you need.
back.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
finish(); }
});
}
You don't need neither onSaveInstanceState nor onRestoreInstanceState.
Just call finish in the onClick listener for the button in the second Activity:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class IntentActivity extends Activity {
EditText ed1, ed2;
float ed1_val, ed2_val;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ed1 = (EditText) findViewById(R.id.editText1);
ed2 = (EditText) findViewById(R.id.editText2);
Button next = (Button) findViewById(R.id.button1);
next.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),
Second_activity.class);
startActivity(intent);
}
});
}
}
This is the second one:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Second_activity extends Activity {
// TODO Auto-generated method stub
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_xml);
Button back = (Button) findViewById(R.id.button1);
back.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
finish();
}
});
}
}
That way you are resuming the previous Activity instead of starting new one.
If you need to pass data between them you could use startActivityForResult / onActivityResult and setResult methods:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class IntentActivity extends Activity {
private static final int GET_VALUES_REQUEST_ID = 1;
public static final String FIRST_VALUE_ID = "first_value_id";
public static final String SECOND_VALUE_ID = "second_value_id";
private static final float DEFAULT_VALUE = 0;
EditText ed1, ed2;
float ed1_val, ed2_val;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ed1 = (EditText) findViewById(R.id.editText1);
ed2 = (EditText) findViewById(R.id.editText2);
Button next = (Button) findViewById(R.id.button1);
next.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),
Second_activity.class);
startActivityForResult(intent, GET_VALUES_REQUEST_ID);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case GET_VALUES_REQUEST_ID: {
if (Activity.RESULT_OK == resultCode) {
ed1_val = data.getFloatExtra(FIRST_VALUE_ID, DEFAULT_VALUE);
ed2_val = data.getFloatExtra(SECOND_VALUE_ID, DEFAULT_VALUE);
setValues();
}
break;
}
}
super.onActivityResult(requestCode, resultCode, data);
}
protected void setValues() {
ed1.setText(Float.toString(ed1_val));
ed2.setText(Float.toString(ed2_val));
}
}
The second activity could be something like that:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Second_activity extends Activity {
// TODO Auto-generated method stub
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_xml);
Button back = (Button) findViewById(R.id.button1);
back.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent data = new Intent();
data.putExtra(IntentActivity.FIRST_VALUE_ID, 324f);
data.putExtra(IntentActivity.SECOND_VALUE_ID, 32234f);
setResult(Activity.RESULT_OK, data);
finish();
}
});
}
}
This is a very basic example so I just hardcoded some return values - please implement something more meaningful.
Beside that you could avoid using underscores as word separator in class names - camel case is much more accepted as name convention.
just finish the second activity don't start it via intent.
When you finish the second activity first activity will be automatically resumed,
you can go for overriding protected void onSaveInstanceState(Bundle outState) and protected void onRestoreInstanceState(Bundle savedInstanceState)
Here is an example
Remember it will work when you will not finish your previous activity.Do this in your first activity.
You are starting a new activity by pressing back button on Second Activity. The new activity instance is not the previous one that has started the Second Activity hence onRestoreInstanceState callback is not getting called.
Its easy if you do not need to send data back to the first activity. How do send back data without using an intent.

Categories

Resources