I have a main activity A -> call activity B -> call activity C -> call activity D.
Activity is called by startActivity(intent).
Activity D have a "Close" button.
How to I notify to Activity A when hit "close" button on Activity D?
Any help is appreciated.
Try this:
Use SharedPreference to store the status of button in Activity D.
In activity A under onResume() get the status of button from the SharedPreference.
//* start the activity with enum defined int activity D to identify your activity
startActivityForResult(intent, DActivity.D_REQUEST_CODE);
//in your D activity define this
private static final int D_REQUEST_CODE = 1;
//* when the button is clicked to close in your D activity, call this in the button event listner
setResult(Activity.RESULT_OK, intent);
//* implement this in your A activity
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if(data==null)return;
if (requestCode == DActivity.D_REQUEST_CODE)
{
switch (resultCode)
{
case Activity.RESULT_OK:
//* do something
break;
default:
break;
}
}
findViewById(R.id.button_close).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(this, Activity_A.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK));
}
});
This will work if you are looking to simply show Activity A while clearing the activity stack. If you just want to know when you return to Activity A, you can save status in SharedPrefs as answered above.
Related
I have a main activity that contains 3 fragments. In each fragment I have a listView, on clicking which it is diverted to a new activity.
In the new Activity, I am changing the list view content that is clicked and diverting back to the main activity using intent.
This opens a new main Activity fragment and when I press back button I again get back to the previous Main Activity that was opened earlier.
Save activity's context in static variable like this
public class MainActivity extends AppCompatActivity {
public static MainActivity mainActivityInstance;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_staff_login);
//finishing older loaded mainActivity
if(MainActivity.mainActivityInstance!=null){
MainActivity.mainActivityInstance.finish();
}
mainActivityInstance=this;
}
}
From other activities you can finish your older activity like this
if(MainActivity.mainActivityInstance!=null){
MainActivity.mainActivityInstance.finish();
}
Instead of using startActivity use startActivityForResult in your fragment and have onActivityResult to obtain result from new Activity you have started
In your fragment do like this
static final int NEXT_ACTIVITY = 100;
//insert in listview on click
Intent intent = new Intent(context,Activity2.class);
intent.putExtra("key","value");//if needed
startActivityForResult(intent,NEXT_ACTIVITY);
#Override //*Note* you must override this method in you parent Activity in order to reflect the data inside your fragment
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode,resultCode,data);//while using inside fragment
switch(requestCode) {
case NEXT_ACTIVITY:
if (resultCode == RESULT_OK) {
Bundle res = data.getExtras();
String result = res.getString("param_result");
Log.d("FIRST", "result:"+result);
}
break;
}
}
In your new Activity while finishing/onBackpressed
Bundle data = new Bundle();
data.putString("param_result", "Thanks Thanks");
setResult(RESULT_OK, data);
finish();
For fragments, instead of finish();, you should call
getActivity().finish();
There are two Activities: ActivityA and ActivityB.
From ActivityA I going to ActivityB. Then, I choose some list item and going back to Activity A(same instance) with selected data. And the most difficult: if I press back now, I should going back to ActivityB(with same instance and with saved View state).
Is it possible to implement? Not necessary via launchMode attribute, perhaps there is another way to manage Activities manually via ActivityManager.
In onActivtyResult of Activity put boolean flag so that you can check that it comes after selecting data from Activity B and in onBackPressed of Activity A start Activity B
like this way
boolean flag = false;
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
flag = true;
}
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
if(flag)
{
//Start Activity B
}
else
{
// finish this activity
}
}
Use startActivityForResult to get the result from activity B which was started by activity A. Activity B returns result when it is finished.
Can you please clarify me, what's the best practice for this example:
Activity1 does INSERTS on SQLite and starts Activity2.
On Activity2, the user clicks on Back button and goes back to Activity1.
But instead of making INSERT, I want to make UPDATE to the data on SQLite.
How do I control, that user "came" back?
What's the best practice?
Assume on button click you starting Activity 2
public static final int YOURCONSTANTNUMBER = 77;
//Button Value
Button second = (Button)findViewById(R.id.firstButton);
second.setOnClickListner(this);
#Override
public void onClick(View v)
{
switch(v.getId())
{
case R.id.btnSecondActivity:
//Code for inserting data into database
Intent secondactivity = new Intent(FirstActivity.this, SecondActivity.class);
startActivity(secondactivity);
break;
}
}
In your second activity
To take control over the back button use the below code
#Override
public void onBackPressed()
{
//Code for update
}
EDIT
Call your second activity with startActivityForResult() method which will let you know the result.
In First Activity
Intent secondactivity = new Intent(FirstActivity.this, SecondActivity.class);
startActivityForResult(secondactivity,YOURCONSTANTNUMBER);
Getting Result
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode)
{
case YOURCONSTANTNUMBER:
// It will execute if you returning from second activity
//Update you database here
}
}
In Your Second Activity
Call this method were appropriate setResult(YOURCONSTANTNUMBER);
Hope it helps
Assuming you want to do both DB operations in Activity1, consider startActivityForResult() here:
http://developer.android.com/reference/android/app/Activity.html#startActivityForResult(android.content.Intent, int)
I have one activity A, that has one button and one list view which shows names of books . on click of the button, activity B starts, there user fill the book form and save it . when he press back button , user comes to activity A. Here the book name should be updated in listview. I think I have to write some code in onResume() . Can u please tell me what to write. I am using customised list view.
Start activity B with startActivityForResult() and use method onActivityResult() to restart or process the new data
For example, to start Activity B:
String callingActivity = context.getLocalClassName();
Intent newActivity = new Intent(getApplicationContext(),ActivityB.class);
newActivity.setData(Uri.parse(callingActivity));
startActivityForResult(newActivity, 0);
Then somewhere in your Activity A class:
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if(requestCode == 0){
// do processing here
}
}
The other answers should suffice, but onResume() can be called in cases where the activity is resumed by other means.
To simply restart Activity A when user presses back button from Activity B, then put the following inside the onActivityResult:
if(requestCode == 0){
finish();
startActivity(starterintent);
}
And in the onCreate of Activity A, add starterintent = getIntent();
Just remember to initiate the variable with Intent starterintent; somewhere before your onCreate is called.
e.g.
public class ActivityA extends ListActivity {
Intent starterintent;
public void onCreate(Bundle b){
starterintent = getIntent();
}
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if(requestCode == 0){
finish();
startActivity(starterintent);
}
}
private void startActivityB(){
String callingActivity = context.getLocalClassName();
Intent newActivity = new Intent(getApplicationContext(),ActivityB.class);
newActivity.setData(Uri.parse(callingActivity));
startActivityForResult(newActivity, 0);
}
}
Then just call startActivityB() from a button click or whatever
YES you are right. Write code in onResume.
When you updated date just call notifyDataSetChanged(); for your ListView adapter
Hope, it help you!
You can either start the activity when user press on Save, and it will fix it for you.
Or if you want to press back:
#Override
public void onResume(){
super.onResume();
list.clear();
list.addAll(getBooks());
adapter.notifyDataSetChanged();
}
In an given Android activity, I would like to start a new activity for the user at some point. Once they leave the first activity and arrive at the second, the first activity is hidden..
Now my question is
I want to bring back the first activity (i dont want to create a new instance of the first activity but to bring back the already existing instance of the first activity) when a button is clicked in the second activity ...
thanks :)
so simple. integrate the below code in your second activity
Button b = (Button)findViewById(yourbuttonid here);
b.setOnClickListener(new View.onClickListener(){
public void onClick(View v){
finish();
}
});
This will work
You would define the first activity with launchMode="singleInstance", then you would start the activity as usual.
Depending on the usage of your second activity, you could also use startActivityForResult() when you start your second activity...
FirstActivity.java{
private static final int SECOND_ACTIVITY = 0;
openSecondActivity(){
Intent forChildIntent= new Intent( this ,FirstActivity. class );
//data for second activity
forChildIntent.putExtra("userName", getUsrName());
this.startActivityForResult(forChildIntent, SECOND_ACTIVITY);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data){
switch (resultCode) {
case RESULT_OK: //do something
default:break;
}
}
SecondActivity.java{
goBackButtonClick(){
Intent retData=new Intent();
//set data to pass back ,if required
//retData.putExtra("userName", getUsrName());
setResult(RESULT_OK, retData);
finish();//will take you to the first activity
}
}