Passing array list from popup window to parent activity - android

I have a popup activity window (using the inflater) and an array
public class PopUp extends Activity{
ArrayList<String> nameSave = new ArrayList<String>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.popup_layout);
getActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.popup_layout, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
public void upload(View view)
{
EditText editText = (EditText) findViewById(R.id.nameBox);
String name = editText.getText().toString();
nameSave.add(name);
}
First of all I'm not even sure if I got the array/list working right. Basically when the person hits the button 'upload' I want it to grab the name and add it to the array. Then I want that array sent back to the previous activity (Launch) which has 2 fragments. One fragment is a listview and I want to put the array's content into that fragment. How do I go about passing this array? Is it as simple as putting it in an intent?

Yes you can put putStringArrayListExtra(name, value) in your intent pass the intent by startActivityForResult(intent, requestCode) and when you done this Activity. You can get the intent by the method described below because the method will be called when your current Activity finished. So get the required data and do what ever you want to do with this data. Or you can also put the array in the intent.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
get your data here and do what ever you want to do with it
}
}
in previous Activity. My be this will help you.

Related

Refreshing Fragment with ListView after theme change on container

I have an app that I'm working on that uses the Twitter API. I have a list view of tweets inside of a Navigation Drawer activity that I added with Android Studio. I have another activity that I use for a settings activity, and on that activity the user can select the theme.
The problem I'm having is that when the user changes the setting and presses the back button, the theme of the container is changed, but not the fragment containing the list view. It's not until I restart the app that the proper theme is applied to the fragment.
However, if I use the back button on the action bar (up navigation) the theme is applied right away.
I'd like to come up with a way to apply the theme again right away when the user presses the back button (And the theme has actually been toggled), and so far this is what I've got:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(requestCode) {
case RETURN_TO_TWEET_LIST:
if (resultCode == RESULT_OK) {
Bundle bundle = data.getExtras();
if (bundle != null) {
boolean themeChanged = bundle.getBoolean(AppConstants.Strings.THEME_CHANGED);
if (themeChanged) {
refreshFragment();
}
}
}
}
super.onActivityResult(requestCode, resultCode, data);
}
private void refreshFragment() {
//TODO: Figure out how to get the list view to refresh when pressing back
}
I feel like the problem is not my Fragment code, as it works the way I would expect in one case, but no the other.
Here is the Navigation Drawer class:
public class CategoryViewSelector extends BaseActivity
implements NavigationDrawerFragment.NavigationDrawerCallbacks {
/**
* Fragment managing the behaviors, interactions and presentation of the navigation drawer.
*/
private NavigationDrawerFragment mNavigationDrawerFragment;
private TwitterFeedFragment currentFragment;
private String fragmentName = "CUSTOM_FRAGMENT";
private CategoryManager categoryManager;
private final int RETURN_TO_TWEET_LIST = 2;
private SettingsManager settingsManager;
/**
* Used to store the last screen title. For use in {#link #restoreActionBar()}.
*/
private CharSequence mTitle;
private String[] drawerItems;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
categoryManager = new CategoryManager();
setContentView(R.layout.activity_category_view_selector);
mNavigationDrawerFragment = (NavigationDrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
mNavigationDrawerFragment.setUp(
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
}
#Override
protected void onResume() {
super.onResume();
}
#Override
public void onNavigationDrawerItemSelected(int position) {
//create a fragment object palceholder
TwitterFeedFragment fragment = null;
// update the main content by replacing fragments
//create a bundle object that will be used to pass arguments to the fragment
//such as a category id.
Bundle bundle = new Bundle();
switch(position) {
case 0:
//load all the tweets by default.
bundle.putLong("categoryId", position);
fragment = new TwitterFeedFragment();
fragment.setArguments(bundle);
setCurrentFragment(fragment);
break;
case 1:
Intent intent = new Intent(this, CategoryManagerView.class);
//may have to change this to be for result again, so that you can use the onActivityResult method
//which will prevent refreshing of the fragment.
startActivityForResult(intent, RETURN_TO_TWEET_LIST);
break;
case 2:
Toast.makeText(getApplicationContext(), "Help not yet available", Toast.LENGTH_LONG).show();
break;
default:
openFragmentForCategory(position);
break;
}
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, currentFragment, fragmentName)
.commitAllowingStateLoss();
}
private void openFragmentForCategory(int position) {
List<Category> categories = categoryManager.getAllCategories();
//there are three default items in the list right now.
//figure out a better way to handle this. Maybe an app constant.
int index = position - 3;
Category selectedCategory = categories.get(index);
long Id = selectedCategory.getId();
Bundle bundle = new Bundle();
TwitterFeedFragment fragment = null;
bundle.putLong("categoryId", Id);
bundle.putString("categoryName", selectedCategory.getCategoryName());
fragment = new TwitterFeedFragment();
fragment.setArguments(bundle);
setCurrentFragment(fragment);
}
/**
* This method attaches a title to the fragment window when an item is clicked. This will also
* require the use of the category names from the category manager.
* #param number
*/
public void onSectionAttached(int number) {
switch (number) {
case 1:
mTitle = getString(R.string.text_category_default_feed);
break;
case 2:
mTitle = "";
break;
case 3:
mTitle = getString(R.string.text_category_help);
break;
}
}
public void restoreActionBar() {
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setTitle(mTitle);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
if (!mNavigationDrawerFragment.isDrawerOpen()) {
// Only show items in the action bar relevant to this screen
// if the drawer is not showing. Otherwise, let the drawer
// decide what to show in the action bar.
getMenuInflater().inflate(R.menu.menu_category_view_selector, menu);
restoreActionBar();
return true;
}
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch(id) {
case R.id.action_settings:
Intent intent = new Intent(this, SettingsActivity.class);
startActivityForResult(intent, RETURN_TO_TWEET_LIST);
break;
case R.id.action_refresh:
currentFragment.refreshFeed();
break;
case R.id.action_logout:
TwitterApplication.getRestClient().clearAccessToken();
onBackPressed();
break;
}
return super.onOptionsItemSelected(item);
}
/**
* This is required so that the refreshFeed method of any fragment can be called.
*
* In the future it might make more sense to make an interface.
* #param currentFragment
*/
public void setCurrentFragment(TwitterFeedFragment currentFragment) {
this.currentFragment = currentFragment;
}
#Override
public void onBackPressed() {
Intent intent = new Intent();
setResult(RESULT_OK, intent);
super.onBackPressed();
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(requestCode) {
case RETURN_TO_TWEET_LIST:
if (resultCode == RESULT_OK) {
Bundle bundle = data.getExtras();
if (bundle != null) {
boolean themeChanged = bundle.getBoolean(AppConstants.Strings.THEME_CHANGED);
if (themeChanged) {
refreshFragment();
}
}
}
}
super.onActivityResult(requestCode, resultCode, data);
}
private void refreshFragment() {
//TODO: Figure out how to get the list view to refresh when pressing back
}
}
Does anyone have suggestions on what I should be doing differently that would allow the fragment to have the correct theme when the back button is pressed? Please let me know if you would like to see more code, or have any questions.
I have tried detaching and attaching the fragment using the fragment manager, but I guess the theme of the fragment is never changed that way.
I'm also open to other suggestions for code improvement when they are spotted.
Thank you very much for any help!
You have to set an Activity's theme in onCreate() before calling setContentView(). And the fragment's theme is set during onCreateView().
What do you mean when you say the container changes theme, but not the fragment? I'm guessing this is what happens ...
1) you go into the SettingsActivity to change theme
2) The host Activity goes through onStop/onCreate, meaning it gets re-themed
3) but you have setRetainInstance(true) on the Fragment, meaning it does not get recreated.
If so, you have two options: remove setRetainInstance, or manually create a completely new Fragment (not just detach and re-attach) when you see that the theme has changed.
A quick fix would be to detach then re-attach the fragment.
Otherwise you could call invalidate() on the specific views of your fragment.

Save selected option drawer activity for use in back button

I have a drawer activity and a method that calls several fragments, when I select an option shows me a listview with buttons. Clicking a button sends me to a new activity that has a back button. I want to return to the previous screen, return to the option you select in the drawer activity.
public class DrawerOpcionesActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.drawer_menu);
....
drawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
mostrarFragment(position);
adapterOpciones.setPositionCheked(position);
}
});
if(savedInstanceState == null){
mostrarFragment(0);
}
...
}
private void mostrarFragment(int position){
Fragment fragment = null;
System.out.println("opcion: " + position);
switch (position) {
case 1:
fragment = new LineasTransporteFragment();
break;
....
}
In the other activity...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.xxxxx);
actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
... //new Intent.. I dont Know who call the save option from activity drawerOptions
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I need to save the selected option, when I call a new Intent show me the selected option. I show the fragment of default position 0
You may use SharedPrefrence of android to save the state and access back the state. Please have a look for sharedPrefrence

Resuming a Tic-tac-toe game in Android after closing the fragment

I'm learning Android development by creating a test project: Tic-tac-toe. My Tic-tac-toe app starts with a Main Menu activity with a Button that says New Game. After clicking New Game, an activity with a fragment containing a Tic-Tac-Toe game (in a GridLayout) launches. The user can play the game, but when they go back to the Main Menu, the game state is not saved.
I want to change this so that when the user goes back to the Main Menu, they will see a new Button called "Continue" (in addition to the "New Game" Button). Once the user clicks "Continue", the game they were playing before continues. If the click the "New Game" button, a new game will be launched like before.
By using the onSaveInstanceState method and a Bundle savedInstanceState, I was able to preserve the game state on orientation changes (by saving the data from the underlying TicTacToe class). My code below shows how I did that. I would like to do something similar this time again - I have read about it, but I don't quite understand the best way to start. Can anyone show me the right steps?
Note that I'm doing this programmatically (i.e. designing the layout outside the xml files)!
My MainMenu class:
public class MainMenu extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void startGame(View view) {
Intent intent = new Intent(this,MainGame.class);
startActivity(intent);
}
}
with a corresponding xml file activity_main_game:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/new_game"
android:onClick="startGame"
/>
</RelativeLayout>
Now my MainGame.class looks like this:
public class MainGame extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_game);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new BoardFragment()).commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_game, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public static class BoardFragment extends Fragment {
public TicTacToe t = new TicTacToe(); //A class I wrote that launches a simple TicTacToe game
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (savedInstanceState != null) {
t = new TicTacToe(savedInstanceState.getInt("TicTacToeData"),
);
}
//Graphics stuff here: variable rootView which contains the TicTacToe grid is defined
//and onClickListeners are added to the ImageViews in the GridLayout which makes corresponding
//changes to t.
return rootView;
}
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putInt("TicTacToeData", t.getGameData());
}
}
}
Thanks for any help.
You have two options
SharedPreferences: you can use shaerdPreferences to store the state of the game. It would be stored also after the restart of the application.
start your game activity with startActivityForResult() and implementing onActivityResult on your parent activity.
There are 2 things you have to do:
Get notified within Main class when your MainGame Activity is closed
Update the UI to change the button text
To get notified within Main class:
You can start MainGame Activity using startActivityForResult instead of startActivity. Then override the onActivityResult method on Main class to be notified when the user close the MainGame.
Also you will have to modify MainGame to invoke setResult(int resultCode, Intent data) or setResult(int resultCode) method when the user click the back button (depending on whether you want to pass additional data back to the Activity.
Override the onBackPressed() method in your MainGame class to invoke the setResult method. E.g.
public void onBackPressed() {
setResult(RESULT_OK);
super.onBackPressed(); // otherwise your Activity won't be closed
}
Have a look at http://developer.android.com/reference/android/app/Activity.html#StartingActivities for a more detailed example
To Update the UI:
Save the button as a local variable (in your onCreate method)
Update the Button text MainGame Activity
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
if (requestCode == PICK_CONTACT_REQUEST) {
if (resultCode == RESULT_OK) {
// Update the button text to "Continue"
} else {
// Change the button text back to "New Game"
}
}
}
One possible solution is to have your MainGame Activity return the state of your game when it finishes. Then your MainMenu Activity can save that value and use it to start up a game in the same state.
For starters, you will want to start your game Activity in a way that enables it to return a value:
public class MainMenu extends ActionBarActivity {
...
public void startGame() {
Intent intent = new Intent(this, MainGame.class);
startActivityForResult(intent, 1);
}
}
Next you will want to make sure that your board Fragment returns the correct state whenever it is closed:
public class BoardFragment extends Fragment {
...
#Override
public void onPause() {
Intent result = new Intent();
result.putExtra("TicTacToeData", t.getGameData());
getActivity().setResult(Activity.RESULT_OK, result);
}
}
Afterwards you will want your menu Activity record the result:
public class MainMenu extends ActionBarActivity {
private int mPreviousGameState;
...
#Override
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
if (requestCode == 1 && resultCode == Activity.RESULT_OK) {
mPreviousGameState = 1;
}
}
}
Now you can pass in this state when you start your game Activity:
public class MainMenu extends ActionBarActivity {
...
public void continueGame() {
Intent intent = new Intent(this, MainGame.class);
intent.putExtra("TicTacToeData", mPreviousGameState);
startActivityForResult(intent, 1);
}
}
Now you will need a way to create a board that is already in a specific game state:
public class BoardFragment extends Fragment {
public TicTacToe t = null
...
public BoardFragment (int gameState) {
t = new TicTacToe(gameState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (savedInstanceState != null) {
// recreate the board after an orientation change
t = new TicTacToe(savedInstanceState.getInt("TicTacToeData"));
} else if (t == null) {
// create a new board if this is a new game
// if this is a continued game, the board is already setup
t = new TicTacToe();
}
}
And lastly you will need to call the correct version of BoardFragment from your game:
public class MainGame extends ActionBarActivity {
...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_game);
if (savedInstanceState == null) {
Intent intent = getIntent();
int gameState = intent.getIntExtra("TicTacToeData", -1);
if (gameState == -1) {
getSupportFragmentManager().beginTransaction().add(R.id.container, new BoardFragment()).commit();
} else {
getSupportFragmentManager().beginTransaction().add(R.id.container, new BoardFragment(gameState)).commit();
}
}
}
}
Another solution is to redesign your app to have only a single Activity (or ActionBarActivity) subclass and multiple Fragment subclasses for each screen you want to show. You can keep a reference to both fragments in your Activity. This will implicitly retain the state of the fragment which displays the game board.

onActivityResult method is not being called

I'm having a problem in my android application. I don't know why 'onActivityResult' method is not being called when 'Up navigation' button from action bar is pressed. I think I've done everything properly:
Parent activity launch child activity with 'startActivityForResult' method.
Intent intent = new Intent(ParentActivity.this, ChildActivity.class);
startActivityForResult(intent, 1000);
Parent activity has overridden 'onActivityResult' method.
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (data != null && requestCode == 1000)
{
Bundle extras = data.getExtras();
Boolean rc = extras.getBoolean(MyConstants.INTENT_EXTRA_RESULT);
if (rc)
{
.......
}
}
}
Child activity has overridden 'onOptionsItemSelected' and calls 'NavUtils.navigateUpFromSameTask'.
public boolean onOptionsItemSelected(MenuItem item)
{
if (item.getItemId() == android.R.id.home)
{
Intent result = new Intent((String)null);
result.putExtra(MyConstants.INTENT_EXTRA_RESULT, true);
setResult(RESULT_OK, result);
NavUtils.navigateUpFromSameTask(this);
return true;
}
else
{
return super.onOptionsItemSelected(item);
}
}
Child activity has overriden 'finish' method. This method set a result.
public void finish()
{
Intent result = new Intent((String)null);
result.putExtra(Constantes.INTENT_EXTRA_HAY_QUE_RECALCULAR, hayQueRecalcular);
setResult(RESULT_OK, result);
super.finish();
}
I'm not sure why 'onActivityResult' method is not being called.
What I've observed is that child activity is not being finished ('finish' method is not being called) when 'Up navigation' button from action bar is pressed. However it is called when back button (hardware button) is pressed.
What I'm doing wrong?
Thanks
As your child Activity is just on the top of your Parent Activity, no need of this method
NavUtils.navigateUpFromSameTask(this);
write like
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
Intent result = new Intent((String) null);
result.putExtra(MyConstants.INTENT_EXTRA_RESULT, true);
setResult(RESULT_OK, result);
finish();
return true;
} else {
return super.onOptionsItemSelected(item);
}
}
finish your Child Activity when home button is pressed.

getIntent is not working inside MainActivity. Getting weird behaviour

I have one MainActivity and I have defined below code in onCreate() method. The intention is, when MainActivity gets extra String "EXIT" then show Toast message:
Intent current = getIntent();
if (current !=null && current.getStringExtra("EXIT") != null) {
Toast.makeText(this, "exiting", Toast.LENGTH_LONG).show();
}
This MainActivity starts another activity "DayOne" on some button press like:
public void processGo(View v){
Intent i = new Intent(MainActivity.this,DayOne.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
MainActivity.this.startActivity(i);
}
Now I am returning back from "DayOne" to MainActivity after putting extra string "EXIT". This I am doing inside onOptionsItemSelected(MenuItem item) method:
public boolean onOptionsItemSelected(MenuItem item){
if(item.getTitle().equals("Exit")){
Intent i = new Intent(DayOne.this,MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.putExtra("EXIT", "EXIT");
startActivity(i);
finish();
}
return super.onOptionsItemSelected(item);
}
The issue is, when MainActivity is getting called from DayOne with extra string "EXIT"; I am not seeing the Toast message defined in MainActivity. What is missing or wrong here?
Appreciate any help.
Thanks all for your comments and helps.
I have figured out the issue here. It was because the manifest file had entry an entry android:launchMode="singleInstance" for Both the activities (MainActivity and DayOne Activity)..
Removing it from there, it worked fine.
First check your null then get the Intent String, can you just make your code look like below within in onCreate of MainActivity#
Bundle extra= getIntent().getExtras();
if(extra!=null){
String _StrExit=extra.getString("EXIT");
if(_StrExit.equalsIgnoreCase("EXIT")){
Toast.makeText(this, "exiting", Toast.LENGTH_LONG).show();
}
}
Update
Make change while calling Intent from menuitem
public boolean onOptionsItemSelected(MenuItem item){
if(item.getTitle().equals("Exit")){
Intent i = new Intent(DayOne.this,MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
String _Str="EXIT";
i.putExtra("EXIT", _Str);
startActivity(i);
finish();
}
return super.onOptionsItemSelected(item);
}
Its working for me.
MainActivity:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent current = getIntent();
if (current != null && current.getStringExtra("EXIT") != null) {
Toast.makeText(this, "exiting", Toast.LENGTH_LONG).show();
}
}
public void processGo(View view) {
Intent i = new Intent(MainActivity.this, OneDayActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
}
OneDayActivity:
public class OneDayActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_day_one);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.day_one, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getTitle().equals("Exit")) {
Intent i = new Intent(this, MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.putExtra("EXIT", "EXIT");
startActivity(i);
finish();
}
return super.onOptionsItemSelected(item);
}
}
dont clear the stack instead of if you want to do some task when you come back to your main activity (using "EXIT" string) you can achieve that using onActivityResult also.:
public void processGo(View v){
Intent i = new Intent(MainActivity.this,DayOne.class);
MainActivity.this.startActivity(i,10); // 10 is the id to handle
}
#Override
public void onActivityResult( int requestCode, int resultCode, Intent data )
{
super.onActivityResult( requestCode, resultCode, data );
switch ( requestCode )
{
case ( 10 ): // id that we pass on start activity
{
if ( resultCode == Activity.RESULT_OK )
{
Toast.makeText(this, "exiting"+data.getStringExtra( "EXIT", "" ), Toast.LENGTH_LONG).show();
}
}
break;
}
}
On finish
public boolean onOptionsItemSelected(MenuItem item){
if(item.getTitle().equals("Exit")){
Intent resultIntent = new Intent();
resultIntent.putExtra( "EXIT", "EXIT" );
setResult( Activity.RESULT_OK, resultIntent );
finish();
}
return super.onOptionsItemSelected(item);
}
on finish it will go back to Main Activity and call onActivityResult automatically where you can do your task.
Sorry for the typo hope it will help.

Categories

Resources