close an activity from other activity? - java

Does anyone know how to close an activity from other activity?? for example: i have 3 activity (activity A, B, and C) and from activity C, i can close an activity A..
my activity structure is activity A -> activity B -> activity C
how to close an activity A from activity C?
i was try this code :
#Override
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent goToLockScreen= new Intent(this,LockScreenForm.class);
startActivity(goToLockScreen);
finish();
but that code is only fot closing activity A from activity B, and can't close activity A from activity C direcly..
Does anyone know about closing an activity direcly from other activity??
thanks..

First Go to parent activity by starting it
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent)
{
super.onActivityResult(requestCode, resultCode, intent);
switch(Code){
case A: go to that activity and finsih() this again come back to parent activity
case B: go to that activity and finsih() this again come back to parent activity
/////and son on
}

try It work perfectly for me
`public class aActivity extends Activity {
public static Activity handleToClose;
#Override
public void onCreate(Bundle savedInstanceState) {
.
.
.
handleToClose = this;
}
public void onClick(View v)
{
Intent i = new Intent(this, act2.class);
startActivity(i);
}
}`
Now You have to finish Activity-A from Activity-B
Activity-B or 2nd Activity
`public class act2 extends Activity {
public void onCreate(Bundle savedInstanceState) {
// your code here
}
public void onClick(View v)
{
aActivity.handleToClose.finish(); //this will finish aActivity (1st Activity)
finish();//to finish current Activity
}
}`

Intent goToLockScreen= new Intent(this,LockScreenForm.class);
goToLockScreen.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
This is a prescribed way and you should follow it.. if you want some other behaviour yo can implement it.. there are lot many questions asked on this topic.. refer other questions...

What about starting both B and C forResult and send a result back to pervious activity to let A finally call finish()? Like this:
A startActivityForResult() -> B startActivityForResult()-> C
C setResult()-> B onActivityResult(){setResult()} -> C onActivityResult(){finish()}
Sounds complicated, but maybe it can be used as a workaround?

try this
If the update activity is launching another installation activity, then you may want to override void onActivityResult(int requestCode, int resultCode, Intent intent) in the update activity, providing the following implementation. Also, when the update activity launches the installation activity, it should do so with startActivityForResult(Intent, int), not with startActivity(Intent).
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent)
{
super.onActivityResult(requestCode, resultCode, intent);
finish();
}

Related

Close MainActivity(main) from another activity (ActivityPoliticas)

I'm looking for a solution to press the (non-personalized ads) button of the ActivityPolicies and close the MainActivity and then reopen the MainActivity with the correct ad.
MainActivity(principal)
public void politicas(View view) // button
{
Intent i = new Intent (this, ActivityPolicies.class);
i.putExtra("valor","politicas");
startActivity(i);
}
ActivityPolicies
public void NonPersonalizedAdvertising(View view) //button
{
SharedPreferences myPreferences = PreferenceManager.getDefaultSharedPreferences(MensajePoliticas.this);
SharedPreferences.Editor myEditor =myPreferences.edit();
myEditor.putString("POLITICAS","LEIDO_NOACEPTADO");
myEditor.commit();
//Missing some option to close the main activity
*
*
*
//Reopen the main activity
Intent entrar=new Intent(this,MainActivity.class);
startActivity(entrar);
finish(); // Close the current activity (ActivityPolicies)
}
The goal is to close the MainActivity to reload the correct personalized or non-personalized ad. I already have the code but I am missing this option to reload the MainActivity.
Another question:
Close the main activity to reload the ads, can I have problems with some Admob rule?
Thank you very much
If your MainActivity is in backstack, you can call same activity like this:
Intent entrar = new Intent(this,MainActivity.class);
entrar.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // this will open the same MainActivity instance and also give you a new intent.
startActivity(entrar);
Now in your MainActivity you can override onNewIntent
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
// here you will get the new Intent. You can reload only the ads programmatically or just call `this.recreate();`
}

How can I change the background color of a Main Activity by using spinner while clicking a button in the Second Activity?

I want to change the background color of Main activity by using spinner in second Activity. I have already created one button and it goes to second activity and in this second activity I have created spinner which consist of which color should be in the main activity. After choosing the color, the button I created will change the background color and will be back to first activity.
From what I understand, you need the ActivityForResult behavior.
You use startActivityForResult to fire the Intent from your first activity to your second, along with a request code.
You use an Intent and setResult to send data from your second activity back to your first.
You override onActivityResult in your fist activity to get and use your data.
Sample code:
public class FirstActivity extends Activity {
private static final int PICK_COLOR_REQUEST = 1001;
...
private void pickColor() {
Intent pickColorIntent = new Intent(this, SecondActivity.class);
startActivityForResult(pickColorIntent, PICK_COLOR_REQUEST);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PICK_COLOR_REQUEST && resultCode == Activity.RESULT_OK) {
int color = data.getIntExtra("color");
/* use the color */
}
}
}
public class SecondActivity extends Activity {
...
private void onColorPicked(int color) {
Intent dataIntent = new Intent();
dataIntent.putExtra("color", color);
setResult(Activity.RESULT_OK,returnIntent);
finish();
}
}

How to get data from 2nd Activity to 1st Activity

my MainActivity is calling a second activity PopupWindow which contains a listview. when user clicks on listview I need to return that information to first activity (MainActivity). So in MainActivity I have this two methods. the first method calls the second activity the second gets result from second activity
//event listener for authors list menu (popup window)
authorListImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent popupWinIntent = new Intent(getBaseContext(), AuthorsPopup.class);
popupWinIntent.putExtra("allauthors", allAuthors);
startActivity(popupWinIntent);
}
});
//fetching result -- author from AuthorsPopup activity back
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if(resultCode == RESULT_OK) {
String author = data.getStringExtra("author");
Log.v("author ", author);
}
}
}
this method is outside the onCreate() method. some tutorials suggest to create the avobe method just like onActivityResul() I'm assuming that case it would be inside the onCreate() method. mine is a method declaration. so obiously not executing. In my second activity. I have this
//event listener for authros listview
authorListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> paren, View view, int position, long id) {
// Log.v("author: ", authorsList[position]);
String author = authorsList[position];
Intent returnResultIntent = new Intent(getBaseContext(), MainActivity.class);
returnResultIntent.putExtra("author", author);
setResult(RESULT_OK, returnResultIntent);
finish();
}
});
what is the proper way to get data back from second activity?
You need to launch the second activity using startActivityForResult(popupWinIntent,1) rather than startActivity(popupWinIntent) and override the onActivityResult method in your first Activity, like this:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == Activity.RESULT_OK){
String result=data.getStringExtra("author");
}
}
}
The first authorListImageView.setOnClickListener code you listed would go in onCreate
1st Activity
How to get value
SharedPreferences sharedPreferences;
sharedPreferences=getSharedPreferences("FileName",0);
sharedPreferences.getString("KEY","DefaultValue");
sharedPreferences.getBoolean("KEY",false);
2nd Activity
How to set or put the value from activity for getting on other activity
SharedPreferences sharedPreferences;
SharedPreferences.Editor editor;
sharedPreferences=getSharedPreferences("FileName",0);
editor=sharedPreferences.edit();
editor.putString("KEY","Value");
editor.putBoolean("KEY",true);
editor.apply();

Android exit whole program syntax

currently I'm working on Android development, now I'm facing a problem to exit the whole application that had launched.
I'v tried .finish(), but it doesn't show what I want.
I have 2 Activities, A and B. Activity A will forward to Activity B when button click. In activity B, when I click button "Exit" (that I created) with the listener to trigger .finish(), it just back to Activity A but not to close whole application (what I want is back to home screen directly and kill the background process as well).
How can I exit whole application wherever in the application? Thank you.
button.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
//here to exit whole application not just backwards to previous activity
}
});
You write in Activity A after
startActivity(new Intent(A.this, B.class))
finish()
then you also write in Activity B finish() on button click listener. It should works.
public class A extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(A.this, B.class));
finish(); // you must write this also in A activity to close whole application
}
});
}
public class B extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_1);
Button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
}
EDIT: I assumed there are cases when the underlying activity either should or shouldn't be terminated on return. This will allow you to handle both cases.
Case A)
Activity A starts activity B, which starts activity C. You want to close all of them from activity C. If they're all in the same task (i.e. probably your case) you can close the whole task by calling
finishAffinity();
According to docs this is what happens:
Finish this activity as well as all activities immediately below it in the current task that have the same affinity. This is typically used when an application can be launched on to another task (such as from an ACTION_VIEW of a content type it understands) and the user has used the up navigation to switch out of the current task and in to its own task. In this case, if the user has navigated down into any other activities of the second application, all of those should be removed from the original task as part of the task switch.
Note that this finish does not allow you to deliver results to the previous activity, and an exception will be thrown if you are trying to do so.
See Activity.finishAffinity().
Case B)
Activity A starts activity B, which starts activity C. You want to close activities B and C from activity C.
This is how you start activity C from B expecting and handling a result:
public class ActivityB extends Activity {
private static final int RC_ACTIVITY_C = 1;
public static final int RESULT_FINISH = 1;
...
public void startActivityC() {
Intent intent = new Intent(this, ActivityC.class);
startActivityForResult(intent, RC_ACTIVITY_C);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_ACTIVITY_C && resultCode == RESULT_FINISH) {
finish();
}
}
}
This is how you let activity B it has to finish from activity C. At any point before finishing activity C call:
setResult(ActivityB.RESULT_FINISH);
See Activity.startActivityForResult(Intent, int) and Activity.setResult(int).
You can try System.exit(0). That should do the job.
EDIT: Take a look at this post for the difference between finish() and System.exit(). Difference between finish() and System.exit(0)

Serial scan using Zxing library - android

i'm developing a scan Barcode app on android , my application is simple and composed from an activity which contains a button and a textView which will receive the result of the scan.. The app works well but i want that i could realise serial scan in a raw. so after scanning a barcode i need that the capture Activity stay and the appli don't back to the button activity so i can scan the next Barcode. any solution please ?
this is my main java code :
public class MainActivity extends Activity {
private Button scan;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scan= (Button)findViewById(R.id.btnScan);
scan.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("com.google.zxing.client.android.SCAN.SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 0);
}
});
}
public boolean onTouchEvent(final MotionEvent event) {
IntentIntegrator integrator = new IntentIntegrator();
integrator.initiateScan(null);
return true;
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
String contents = data.getStringExtra("SCAN_RESULT");
TextView tv = (TextView) findViewById(R.id.scanResult);
tv.setText(data.getStringExtra("SCAN_RESULT"));//this is the result
} else
if (resultCode == RESULT_CANCELED) {
// Handle cancel
} }
}
#Override
public void onConfigurationChanged(Configuration newConfig){
super.onConfigurationChanged(newConfig);
}
}
I had the same problem when I did my scanner activity, and the solution that I found was to make my mainActivity extends Zxing CaptureActivity, like this I overrided handleDecode and I avoided to switch between different activities (as you have to do to obtain your scanner result).
Anyhow, to restart the scanning process after a precedent scan I called the method
restartPreviewAfterDelay(0L)
(that is a method of CaptureActivity) in the onClick function of a button.
Take a look at that method, I think that it is what you need.
i finally found the solution i had just to add this code in the onActivityResult() declaration
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("com.google.zxing.client.android.SCAN.SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 0);
So after the scan is finished the app is ready to scan again instead of going back to the home activity

Categories

Resources