I have my MainActivity, and I create an Intent to a SecondActivity, when a button is pressed,
I want to return to activity. But when I create the intent and pass (this, MainActivity.class) through the constructor, I get an error: The intent constructor is undefined. Can someone help me?
public void goBackToMainActivity(View view) {
//Button for retrieving the user's current location:
final Button backButton = (Button) findViewById(R.id.back_button);
//Listens for button presses and releases:
backButton.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
//When the button is pressed:
if(event.getAction() == MotionEvent.ACTION_DOWN) {
//Change image for touch indication:
backButton.setBackgroundResource(R.drawable.back_button);
//When the button is released:
} else if (event.getAction() == MotionEvent.ACTION_UP) {
//Change image back to default:
backButton.setBackgroundResource(R.drawable.back_button_selected);
goBackToMainActivity();
}
return false;
}
});
}
public void goBackToMainActivity() {
finish();
Intent startIntent = new Intent(this, MainActivity.class);
startActivity(startIntent);
}
I guess you are creating the intent inside an OnClickListener, thus you need to use the code below:
Intent intent = new Intent(SecondActivity.this, MainActivity.class);
The reason is that if you do not specify SecondActivity.this, you are actually passing the OnClickListener into the intent constructor.
Change your goback method:
public void goBackToMainActivity() {
Intent startIntent = new Intent(CurrentActivityName.this, MainActivity.class);
startActivity(startIntent);
finish();
}
First clear all the existing activities by writing code:
startActivity(intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
Then call the intent to the MainActivity
Intent i=new Intent(CurrentActivity.this, MainActivity.class);
startActivity(i);
Try calling finish() on button click in second activity to return to the main activity.
public void onClick(View v) {
finish();
}
Related
I have a problem with my simple login app using SharingPreferences.
Imagine,that user logs in to application. He has "Log out" button inside his personal account. When user already logged in then he shouldn't return to login activity when he clicks "Back" button on his smartphone, he should return only then he clicks log out button and then he may fill fields with other data(other login,other password).
So, I can't finish() my first activity,because it is possible to return back.
Doesn't work ( it is impossible to return back to login (MainActivity) when I click LOG OUT button.
signInButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
sharedPreferencesEditor.putString("user_login",login.getText().toString());
sharedPreferencesEditor.putString("user_password",password.getText().toString());
sharedPreferencesEditor.apply();
Log.d("MyLog_name",sharedPreferences.getString("user_login",""));
Log.d("MyLog_pw",sharedPreferences.getString("user_password",""));
Log.d("MyTag",login.getText().toString());
Log.d("MyTag",password.getText().toString());
if(login.getText().toString().equals(getString(R.string.user_name)) && password.getText().toString().equals(getString(R.string.user_pw))){
Intent intent = new Intent(MainActivity.this, SuccessActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivityForResult(intent,1);
finish();
} else {
Toast.makeText(getApplicationContext(),"Wrong data(очищаю пароль)",Toast.LENGTH_SHORT).show();
password.setText("");
}
}
});
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 1 && resultCode == RESULT_OK && data!=null){
String savedUser = data.getStringExtra("username");
Toast.makeText(getApplicationContext(),"Farewell, " + savedUser,Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(),"Error(Back button was pressed, no prohibition of back stack)",Toast.LENGTH_SHORT).show();
}
}
SuccessActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_success);
welcome = (TextView)findViewById(R.id.welcome);
logOut = (Button) findViewById(R.id.logOut);
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
savedUserName = sharedPreferences.getString("user_login","");
welcome.setText("Welcome, " + savedUserName);
logOut.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent();
intent.putExtra("username",savedUserName);
setResult(RESULT_OK, intent);
finish();
}
});
}
Don't finish activity when you are using startActivityForResult() method just put your code ->
/////////
Intent intent = new Intent();
intent.putExtra("username",savedUserName);
setResult(RESULT_OK, intent);
finish();
///////////
in logOut click and on back pressed to get back to the Log In Activity
You will have to finish the LoginActivity while going for SuccessActivity and while clicking on Logout button you can start the LoginActivity once again Over there you can pass the data from SuccessActivity to LoginActivity and do needfull by checking if getIntent.getExtras() are null or not or you can check getIntent.hasExtra()
In my app, there are so many activities which are referred to as levels. And one activity is Reward activity. when i win level-1, reward activity opens. Now i want to replay the level-1. For this i have used getExtra(). My app crashes when i click the replay button.
Houselevel1.java
public void getReward(){
if(count == 3) {
Intent intent = new Intent("com.creatives.arfa.revealthesecretsgame.Reward");
intent.putExtra("activity", "level1");
startActivity(intent);
}
}
HouseLevel2.java
public void getReward(){
if(count == 3) {
Intent intent = new Intent("com.creatives.arfa.revealthesecretsgame.Reward");
intent.putExtra("activity", "level2");
startActivity(intent);
}
}
Reward.java
public void replayLevel() {
replay = (ImageButton) findViewById(R.id.replay);
Intent intent= getIntent();
activity = intent.getStringExtra("activity");
replay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View paramView) {
if(activity.equals("level2")){
Intent intent = new Intent("com.creatives.arfa.revealthesecretsgame.HouseLevel2");
startActivity(intent);
}
if(activity.equals("level1")){
Intent intent = new Intent("com.creatives.arfa.revealthesecretsgame.Houselevel1");
startActivity(intent);
}
}
});
}
With the java code you've posted, in the Reward.java file, you're trying to create another Intent Object with the same name as the one declared in the scope right above it. Because of this, the build will never be successful.
Also, when you declare intents, you MUST pass on the activity_name.class file.
Something you can try:
1) HouseLevel1.java
public void getReward(){
if(count == 3) {
Intent intent = new Intent(getApplicationContext(), com.creatives.arfa.revealthesecretsgame.Reward.class);
intent.putExtra("activity", "level1");
startActivity(intent);
}
}
2) HouseLevel2.java
public void getReward(){
if(count == 3) {
Intent intent = new Intent(getApplicationContext(), com.creatives.arfa.revealthesecretsgame.Reward.class);
intent.putExtra("activity", "level2");
startActivity(intent);
}
}
3) Reward.java
public void replayLevel() {
replay = (ImageButton) findViewById(R.id.replay);
Intent intent= getIntent();
activity = intent.getStringExtra("activity");
replay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View paramView) {
if(activity.equals("level2")){
Intent intent = new Intent(getApplicationContext(), com.creatives.arfa.revealthesecretsgame.HouseLevel2.class);
startActivity(intent);
}
else if(activity.equals("level1")){
Intent intent = new Intent(getApplicationContext(), com.creatives.arfa.revealthesecretsgame.Houselevel1.class);
startActivity(intent);
}
}
});
}
Also, if you're simply using the Reward.java file to get the previous intent's data, perform some calculation, and send some data back to the calling, or parent activity, then you can simply use the startActivityForResult() method, which takes care what what you're trying to do manually.
Here's a small article that might be able to help you with the problem
http://www.vogella.com/tutorials/AndroidIntent/article.html#retrieving-result-data-from-a-sub-activity
If all you want is go from Activity 1 or to 2 to a Reward activity grab something and send that something back to either activity.
What you do is startActivityForResult You pass an Id (constant number) do what you do on the Reward activty, pack what you need to return in a Bundle, and set ActivtyResult to OK and close your activity.
Your app will go back to the Activity1 or 2 whoever call it. On those activties you override the method onActivityResult There you check if the id on which the result is coming from is the Id you sent on the startActivityForResult and if the status is OK.
Then you have whatever was set on the Reward activity. The Reward activity don't need to know from where it came from if only will grab some data. So you can later have an Activity3 that calls the Reward activity and you do not need to modify the Reward activity.
It is explain here check the accepted answer.
How to manage `startActivityForResult` on Android?
I have 2 Activities. On 2nd activity i have data which i want to move by button "Back" to 1st Activity.
Usually i moving by something like this:
button12.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
pass=editText2.getText().toString();
Intent intent = new Intent(FirstActivity.this, MapsActivity.class);
intent.putExtra("pass_value", pass);
startActivity(intent);
}
});
But now i dont want to start Activity, instead of this i want close Activity, so i need to use finish()
Currently i created this, but no working:
b2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, FirstActivity.class);
intent.putExtra("number", numberOfTrue);
finish();
}
});
I need something more in this code, but i dont know what.
Start a new activity with
Intent intent = new Intent(FirstActivity.this, MapsActivity.class);
intent.putExtra("pass_value", pass);
startActivityForResult(intent,1)
You can use setResult method to acheive your desired result
Intent output = new Intent();
output.putExtra("number", numberOfTrue);
setResult(Activity.RESULT_OK, output);
finish();
get Your result in FirstActivity
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1 && resultCode == Activity.RESULT_OK && data != null) {
int num = data.getIntExtra("pass_value");
}
}
b2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
finish();
Intent intent = new Intent(this, FirstActivity.class);
intent.putExtra("number", numberOfTrue); }});
You need to open by function startActivityForResult
startActivityForResult(Intent intent, int requestCode)
And get the data returned in the function
onActivityResult
As an example you can see a face to the camera for a picture
I'm trying to build a simple login & registration application in android. My problem is that I want to handle backpressed() but it is not working.
Example: I'm having 3 activities: signin, register and verify. What I need is if we back click on register or verify, the navigation should go to signin and if we back click on signin, the application should close.
I have tried these lines of code but they are not working. They will navigate to previously visited activity.
This code is of signin activity
#Override
public void onBackPressed() {
Intent i = new Intent();
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
finish();
}
This code is of register activity
#Override
public void onBackPressed() {
Intent i = new Intent(getBaseContext(), LoginActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
What I need is that if we back click on register or verify, the
navigation should go to signin and if we back click on signin, the
application should close.
To go to SignInActivity onBackpressd() from Verify and RegisterActivity:
#Override
public void onBackPressed() {
Intent i = new Intent(this, SignInActivity.class);
finishAffinity();
startActivity(i);
}
In SignInActivity:
#Override
public void onBackPressed() {
finish();
}
Hope this helps.
If you're building for API>16
#Override
public void onBackPressed(){
finishAffinity();
super.onBackPressed();
}
This works for sure!!
#Override
public void onBackPressed() {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finishAffinity();
System.exit(0);
}
Finish will always go to the previous activity on the stack. The intent flags need to be set when your activity is LAUNCHED, not when your activity is being killed.
I am trying to switch activity's with the use of a button.
Skillz.java
Button b2 =(Button)findViewById(R.id.button2);
b2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent myintent = new Intent();
String packageName="marco.skillz.app";
String className="marco.skillz.app.act2";
myintent.setClassName(packageName, className);
startActivity(myintent);
}
});
act2.java
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.page2);
}
When the app run in the emulator I get the following error:
The application "app name" (process marco.skillz.app) has stopped unexpectedly.
FIXED!! I feel so stupid i had android:name=".act1" when it should be android:name=".act2".
Thanks for all your input :P
Please check like this
public void onClick(View v) {
Intent myintent = new Intent(Skillz.this,act2.class);
startActivity(myintent);
}
Add act2 activity in the manifest file
Try this Skillz.java in oncreate
Button b2 =(Button)findViewById(R.id.button2);
b2.setOnClickListener(new OnClickListener()
{public void onClick
(View v) {
Intent i = new Intent(getApplicationContext(), act2.class);
startActivity(i);
}
});