Helping to create an app, very new to using android studio and as a programmer. Just wondering if anyone could help with this current issue.
This is a secure server communication app.
On the app there is a back button on the conversation screen which should take you back to the conversation thread. Once you kill the app on your phone and open a message on the app through a notification it takes you directly to the conversation screen, but as you hit the back button instead of going back to the conversation thread it takes you off the app completely which is not what we want.
Can someone please tell me whereabouts I should go to fix this problem and the code to do so.
Thank you
It because once you open activity from the notification will be root activity. and hitting back button just close the app.
You need to check for the root activity. Try this method isTaskRoot().
Once you open activity initial check for that the activity is root or not. If it is root then call startActivity() else if not root call back functionality.
So for example:
YourBackButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(isTaskRoot()){ //Check if it is your root activity
Intent intent = new Intent(Current_class.this,Conversation_thread.class);
startActivity(intent);
finish();
}
else{
finish();
}
}
});
public void onBackPressed() {
//start your conversation thread class here. something like this
Intent intent=new Intetn(Current_class.this,Conversation_thread.class);
startActivity(intent);
}
Hope this helps you!.
Related
I have 2 screens corresponding to 2 activities on my Android app. I am trying to go back to the first activity from the second screen by clicking a Back button. Even though my first activity restarts, I get an annoying pop-up message "Unfortunately the app has stopped" as my app refreshes to go back to the first activity. I would like to not have this message appear.
I have tried different variations of code and the message appears each time :
#Override
public void onBackPressed() {
startActivity(new Intent(this, MainActivity.class));
}
or
public void finishActivity(DisplayMessageActivity v){
Intent intent = new Intent( this, MainActivity.class );
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
}
or even just
public void finishActivity(DisplayMessageActivity v){
finish();
}
You should read the logs to find out what the exception is and why it is occurring. Places to check:
If back navigation logic is inside an OnClickListener, something inside the OnCLickListener code is probably null.
The activity it should go to after pressing back is having a null value for something you're trying to use.
Any asynchronous calls from old activity finishing after it has been removed.
Your log should tell you exactly what it is.
i have two main Activity with two separate XML file that design for two languages.
when someone tap on Image button on Main Activity ENG ,it will change and another activity with different layout and language shows up.
the point is, i have on Back Pressed() method that if user pressed back button twice , it should exit the app.
now what i get is when tap a lot and switch between two activity the back pressed button wants to back to previous activity to the end that it seems absolutely right but i need to quit directly. what can i do?
public void onBackPressed()
{
if(count == 1)
{
count=0;
finish();
}
else
{
Toast.makeText(getApplicationContext(), "Press Back again to quit.", Toast.LENGTH_SHORT).show();
count++;
super.onBackPressed();
}
return;
}
thanx
Simple scenario, You start Your Activity B from Activity A by button press:
yourButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v){
Intent i = new Intent(YourActivity.this,YourNextActivity.class);
startActivity(i);
finish();//finish Your first activity
}
});
The only thing You have to do is, to finish the first Activity from where You get to the next one. Then Activity B has no chance to go back to Activity A and the app "finishs".
This is an article about exit and quit in android apps.
http://android.nextapp.com/site/fx/doc/exit
but for your app purposes you code use these lines:
finish();
moveTaskToBack(true);
System.exit(0);
This is the correct behaviour, to quit, you'd have to call finish() on both activities. However, if you create the second Activity with the Intent.FLAG_ACTIVITY_CLEAR_TOP flag, you'll be able to quit as described. However, your users won't be able to go back in the activity hierarchy - are you sure you want that?
Anyways, this way you'll be able to quit:
final Intent intent = new Intent(this, SecondActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Redirect to home. I opened different App from my App using intent. When I click back it was redirecting to my application. But at that time I want to close that App or redirect me to home. I tried with finish() and destroy(). But those are not working ?
Intent nextIntent = new Intent(Intent.ACTION_MAIN);
nextIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
nextIntent.setComponent(new ComponentName(packageName,package_running_cls));
NotFirst.this.finish();
startActivity(nextIntent);
If finish() is not working you should override onBackpressed() method and in onbackpressed you can use intent to go to your HomeActivity like this
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
Intent in=new Intent(this,YourHomeActivity.class);
startActivity(in);
this.finish();
}
try this, it may help you.
You can use moveTaskToBack(true).
This will hide your application until the user wants to use it again.
The longer answer starts with another question: why do you want to kill your application?
The Android OS handles memory management and processes and so on so my advice is just let Android worry about this for you. If the user wants to leave your application they can press the Home button and your application will effectively disappear. If the phone needs more memory later the OS will terminate your application then.
As long as you're responding to lifecycle events appropriately, neither you nor the user needs to care if your application is still running or not.
So if you want to hide your application call moveTaskToBack() and let Android decide when to kill it.
I am doing one application here I have one exit button when I click button,that time I need to exit from application..I tried using below code app is closing but it still running in taslmanager..but when I click exit button I should close app as well as I should remove from task manager how it's possible.
public class MainMenu extends Activity {
Button exit;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainmenu);
exit=(Button)findViewById(R.id.btn1);
exit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
});
}
}
try this to exit the application
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
finish();
Please think really hard about if you do need to kill the application: why not let the OS figure out where and when to free the resources?
Otherwise, if you're absolutely really sure, use
finish();
As a reaction to #dave appleton's comment: First thing read the big question/answer combo #gabriel posted: Quitting an application - is that frowned upon?
Now assuming we have that, the question here still has an answer,
being that the code you need if you are doing anything with quitting
is finish(). Obviously you can have more than one activity etc etc,
but that's not the point. Lets run by some of the use-cases
You want to let the user quit everything because of memory usage and
"not running in the background? Doubtfull. Let the user stop certain
activities in the background, but let the OS kill any unneeded
recourses. You want a user to not go to the previous activity of your
app? Well, either configure it so it doesn't, or you need an extra
option. If most of the time the back=previous-activity works,
wouldn't the user just press home if he/she wants to do something
else? If you need some sort of reset, you can find out if/how/etc
your application was quit, and if your activity gets focus again you
can take action on that, showing a fresh screen instead of restarting
where you were. So in the end, ofcourse, finish() doesn't kill
everthing, but it is still the tool you need I think. If there is a
usecase for "kill all activities", I haven't found it yet
Use this flag:
FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS.
This flag will notify the OS to remove this app/activity from the cache of recent apps.
intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
You can also add
android:noHistory="true"
android:excludeFromRecents="true"
in your AndroidManifest file for the activity where you don't want to see in recent apps.
Android doesn't allow you to directly exit from the app.you just have to remove the your current activty from the stack before going to any new activity.
class first extends activity{
oncreate(){
Intent i = new Intent(getappcontext(),nextactivity);
stratactivity(i);
first.this.finish();
}
}
I have one problem regarding my app,when i click back button my application is getting finish.i need to prevent it.How could i make it .
#Override
public void onBackPressed() {
Log.d("CDA", "onBackPressed Called");
Intent setIntent = new Intent(Intent.ACTION_MAIN);
startActivity(setIntent);
}
I have tried something like this.But its not working .Actually what i need is when i will click back button ,the application should not finish it should run in background.can anybody help me out.#Thanks
Try doing it like this:
#Override
public void onBackPressed() {
Log.d("CDA", "onBackPressed Called");
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
}
By doing this, your activity will not be destroyed (i.e. onDestroy will not be raised). But also, there's no guarantee that Android will preserve your activity for long.
In case, you are running a process that you want to keep on running even in background, then I would suggest you to go for Service or IntentService.
What do you mean by "it should run in background"? Why would you want that? If the user wants to keep the app opened, he can use the home button and the app won't be closed, if he presses the back button he wants to close the app. If want to have something that is still running even after the user closes the application you should take a look at Service http://developer.android.com/reference/android/app/Service.html, this will continue running even after the user closes the app