I am not sure this workaround is the correct way to achieve my goal of having a prompt text in a spinner. What happens with this application is the spinner navigates to another Activity via an Intent and when the user navigates back to the Main Activity with the spinner they have two ways back. One with a Button and a click event the other by clicking the device BACK button. I am trying to call the code in the click event from the method that manages the device BACK button
I do not know how to call the click event from the device BACK button Method
#Override
public void onBackPressed() {
Toast.makeText(getApplicationContext(),"Use BACK BUTTON\n\n"+"On the Screen",Toast.LENGTH_LONG).show();
// I want to call goBack(View view) from here
// +++++++++++++++++++++++++++++++++++++++++++
}
public void goBack(View view){
Intent i = new Intent( PageTwo.this, MainActivity.class );
startActivity( i );
}
The reason I use this Intent to navigate BACK to the Main Activity is it reloads the variables in the Spinner
It looks like goBack(View) is most likely from an onClick setup in your layout XML. Since you aren't using the view, just pass null:
#Override public void onBackPressed() {
goBack(null);
}
I don't know if I get you right, if you just want to go back to the activity which started another activity, you can just call finish() method of Activity class:
#Override
public void onBackPressed() {
finish();
}
finish() reference
Related
I'm very new to Android Studio Development and I was wondering how to do this, when I click a button on MainActivity, it will direct me to secondActivity where the text become visible (Originally TextView will not be visible until the button from MainActivity is pressed)
imageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
String status = "Success!";
intent2.putExtra("Status",status);
startActivity(intent);
}
});
I want to make an if-else statement for this (on SecondActivity page) where if user straight away go to SecondActivity, it will not display any text there. But if pressed the button on MainAcitivty page, the system will go to SecondActivity with the TextView displayed.
Thanks!
Basically, there are several approaches you can do that.
Use intents pass data
Sure you pass a boolean type or whatever you want into this intent, I think this is the approach you are trying to make here. So I can give you an example:
In your first activity you can do something like this,
button.setOnClickListener {
val intent = Intent(this#MainActivity, SecondActivity::class.java).apply {
val status = true
putExtra("Status", status)
}
startActivity(intent)
}
And in your second activity, in your need to override onCreate to parse your intents to decide your text want to display or not.
val status = intent.extras?.getBoolean("Status")
if(status) {
hideText()
} else {
showText()
}
the other approach you can deal with it is try to create singleton class to keep the status in this class, and based this singleton class status, you may choose to hide/show your text. However this solution isn't the recommended way to do it. Because global state is bad for testing and just pollute the code.
I have a subActivity that can be open from my mainActivity.
For some reasons, when the user clicks on the back button go back to my mainActivity, I want my subActivity to remain open in background in order to be able to come back for later.
Questions:
how to avoid to close the subActivity when the user clicks back ?
how to come back to the mainActivity without restarting it ?
how to come back later to my opened activity without re-creating it completely ? (just want to bring it to front)
Thanks !
On you subActivity onBackPressed() add this
#Override
public void onBackPressed() {
Intent i = new Intent(SubActivity.this, MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(i);
}
on mainActivity :
private void openSubActivity() {
Intent intent = new Intent(MainActivity.this,SubActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
}
override onBackPressed() and remove super from it then try opening the activity which you want to open from there for ex-
#Override
public void onBackPressed() {
// your code
}
and with the help of different activity launch modes you can achieve it
I am Calling a function(present in fragment) from activity when back button is pressed
public void onBackPressed() {
new Home().show(Home.home_list,app.this);
}
Home is fragment of app activity
In show function I am calling an intent
public void show(final ArrayList<tile_data> data, final Activity activity) {
startActivity(new Intent(activity, Chat_topic_layout_for_user_group.class)
}
In doing so it gives me error : java.lang.IllegalStateException: Fragment Home{5deabab} not attached to Activity
My conclusion is that app is using show as function so it doesn't know about home.
So my question is how to call an "intent" present in some function in some activity or fragment from other activity??
Looks like startActivity is not being called in the right scope. Try specifying the scope and calling it like this: activity.startActivity(new Intent(activity, Chat_topic_layout_for_user_group.class)
Why don't you move the Show() function to the Activity? And for the titles create a getter function in the Home fragment, getTitles(). Now, when the event occurs trigger the Show() function directly in the activity passing (*HomeFragment*.getTitle() , *Activity*.this). Are your titles static? Because you are directly calling new Home(). If that's the case there won't be any issue in calling Show() function in the activity. You can explain your requirements in detail if this isn't up to par.
public void show(final ArrayList<tile_data> data, final Activity activity) {
activity.startActivity(new Intent(activity, Chat_topic_layout_for_user_group.class));
}
I am simply trying to click back and navigate to my previous activity. My flow is this: I go to news_feed activity -> Comments activity -> User Profile activity -> click back (Go to Comments activity) -> click back (This does nothing for some reason) -> Click back (Go back to news_feed activity). I'm not sure why I have to click back twice when I try to go from Comments activity back to news_feed activity. If I go from news_feed activity -> Comments -> press back (Go to news_feed activity) this works perfectly. Here is my code:
news_feed.java:
#Override
public void onBackPressed() {
super.onBackPressed();
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
}
Comments.java:
#Override
public void onBackPressed() {
super.onBackPressed();
this.finish();
}
UserProfile.java:
#Override
public void onBackPressed() {
super.onBackPressed();
Intent intent = new Intent(UserProfile.this, Comments.class);
Bundle bundle = new Bundle();
bundle.putString("postId", postId);
bundle.putString("posterUserId", posterUserId);
bundle.putString("posterName", posterName);
bundle.putString("postStatus", postStatus);
bundle.putString("postTimeStamp", postTimeStamp);
bundle.putString("postTitle", postTitle);
intent.putExtras(bundle);
startActivity(intent);
finish();
}
I don't think navigating to these activities would change anything, but I can include the intents that I used to navigate to these activities also if necessary. Otherwise I just included the onBackPressed code that I had for these activities. Any ideas why this might cause a problem? Any help would be appreciated. Thanks.
In your UserProfile class, in onBackPressed() method, you are starting the Comments class again. Why do you have to do this?
What is happening is, you are starting a new Comments activity onBackPressed() of the USerProfile class, so there are two instances of Comments Activity. So you feel you are pressing back btn twice.
If you have to pass data back to Comments from UserProfile class, then make use of setResult() method.
This will be of help
How to pass data from 2nd activity to 1st activity when pressed back? - android
Sending data back to the Main Activity in android
I you want to send back the result to previous activity the start activity by using
startActivityForResult()
insead of
startActivity()
and don't override
onBackPressed()
You can try something like this:
private boolean clicked;
#Override
public void onBackPressed() {
if (!clicked) { // if it was not clicked before, change the value of clicked to true and do nothing directly return, if clicked again, then it will be finish the activity.
clicked=true;
return;
}
super.onBackPressed();
}
And if you are going to use the time, then you can do like this:
private long lastClicked;
#Override
public void onBackPressed() {
if (System.currentTimeMillis() - lastClicked < 1000) { // within one second
super.onBackPressed();
}
lastClicked = System.currentTimeMillis();
}
You don't need to override onBackPressed() if all you're going to do is call startActivity() and finish() in the current activity. The Android backstack will handle both of those for you. Moreover, in your UserProfile.java you are passing data to previous activity. For this, you should use startActvityForResult() in the Comments activity (i.e the previous activity) instead of startActivity().
To learn more about startActvityForResult() visit: https://developer.android.com/training/basics/intents/result.html
public void onBackPressed() {
if (this.lastBackPressTime < System.currentTimeMillis() - 2000) {
this.lastBackPressTime = System.currentTimeMillis();
} else {
//super.onBackPressed(); for exit
//INTENT HERE TO YOUR SECOND ACTIVITY like below
Intent intent=new Intent(A.this,B.class);
startActivirty(intent);
}
}
Create a global variable
private long lastBackPressTime = 0;
Hope this will help you,Let me know..
I am trying to implement simple back button on activity to go to previous activity, but some weird behavior is happening when I am calling method finish() - the problem is I have to press back button TWO times? Why is this happening? In the back method i have only finish(). What is the other way to go to previous saved activity without instantiate a completely new Intent?
public void back1 (View view){
this.finish();
}
This is second try with the same result:
ImageButton buttonback = (ImageButton) findViewById(R.id.imageButton6);
buttonback.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
onBackPressed();
}
});
You are most likely starting the activity twice.
When pressing back you are finishing one, and the other one is coming forward. Check your onCreate and onResume for anything that may be using intents or starting any activity
#Override
public void onBackPressed() {
super.onBackPressed();
}
add that to your code the super call closes the activity no need to call finish()
If you just want the devices back button to function, you dont need to override the onBackPressed method in Activity.
If you want to place a custom button in view and set back action to that button, then you need to write button clicklistener to the same
backbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
finish();
}
});