I have scenario with two screens.
Screen 1 shows data from from API in list format
There is a "+" button in menu bar
Clicking this button takes user to screen 2
User can enter some info on screen 2 and press the "save" button on top of this screen. This does a POST to my API and saves the data.
After saving, I would like to put the user back to screen 1. I've done that with this:
#Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
if (menuItem.getTitle().toString().equalsIgnoreCase("save")) {
new CreateSomethingTask(this,enteredName.getText().toString(), id).execute();
Intent listscreen = new Intent(getApplicationContext(), ShowListActivity.class);
startActivity(listscreen);
return true;
}
return true;
}
However, the added item is not shown. If I close my app and open it again then the item shows up.
Is there a good way to handle this? I like how the Github Android app handles this when creating a new Gist. But I'm not sure how to implement that.
You should start your screen2 with startActivityForResult(). That way you could send a result back and a code and proceed to refresh your screen1. See example : How to manage `startActivityForResult` on Android?
Below function maybe help you, didn't tried.
#Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
callFunctionToRefreshList();
//or redraw data from api
//setContentView(R.layout.activity_book);
}
Related
Hey guys I'm making a pocketbook in Unity. On the upper right side corner there's a menu button that I've coded to make a drop-down list of links appear that will bring you to different pages of the pocketbook. Right now the only way to make the list dissapear is to click on the menu button again, which is inconvenient. I want the user to be able disable the menu by tapping anywhere else on the screen (I've created an invisible button that covers the rest of the screen, only problem is I want it to be active only when the scroll menu is active) like a normal menu screen. This is the code I've created for it. Menu Button is the menu button, close button is a button I created that covers the rest of the screen, I'm struggling to make the button active only when the scroll menu is up otherwise it will stop the user from scrolling through the pages. Thanks
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class MenuButton : MonoBehaviour {
public GameObject scrollMenu;
public Button menuButton, closeButton;
public bool active = false;
// Use this for initialization
void Start () {
Button btn = menuButton.GetComponent<Button>();
btn.onClick.AddListener(TaskOnClick);
closeButton.enabled = false;
}
// Update is called once per frame
public void TaskOnClick ()
{
if (active == false)
{
scrollMenu.SetActive(true);
active = true;
closeButton.GetComponent<Button>().interactable = true;
} else
{
scrollMenu.SetActive(false);
active = false;
}
}
}
What you can try is.
1. Make a button part of your menu, it should cover the entire screen
2. it must visible/hide with your menu
3. it must be transparent.
4. on the button click event attach menu hide code.
I have done that and it's working perfectly.
see the image below
I'm implementing an Android application with a Navigation Drawer and I have a problem with it.
I started from the template (Android app with navigation drawer) in Android studio and added a new section to it - Settings. When clicking the settings button the user is taken to a new Activity and the user can go BACK either by pressing the "up" button in the action bar (Which works perfectly) or by pressing the physical back button. That part doesn't work so good.
When pressing the "up" button and then opening the navigation bar, my home page is highlighted (selected). But when using the back button and opening the navigation drawer, the Settings item is selected, as if the user is viewing that page and not the home page.
So this is because the Navigation Drawer fragment isn't updated and I havent found any way to solve this problem. What I would like is for the Navigation Drawer to be recreated (Which is what I think happens when pressing the "up" button). Do you know how I can make this happen?
I started working on a solution based on this:
getSupportFragmentManager().addOnBackStackChangedListener(
new FragmentManager.OnBackStackChangedListener() {
public void onBackStackChanged() {
// Update your UI here.
}
});
But I don't know what to write there that will update the whole UI. (experimented with calling onCreate() but it's too ugly and can't be the right way).
Any suggestions?
UPDATE:
The onBackStackChanged event isn't sent when pressing BACK from another activity. Only for fragments in the current actvity.
You have to override the actionbar back button like this :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case android.R.id.home:
this.finish();
//or
super.onBackPressed();
break;
}
return true;
}
I had the same problem.I used notifyDataSetChanged() in the ListView Adapter.
In getView() i used,
if(mListView.isItemChecked(position)) {
convertView.setBackgroundColor(getResources().getColor(R.color.md_black_1000_12));
}else{
convertView.setBackgroundColor(getResources().getColor(android.R.color.transparent));
}
notifyDataSetChanged();
First of all i am learning android, I am testing my Andorid app in my Samsung Galaxy S1.
My app Function is: while i am pressing RandomNumber button, it will generate Random numbers and displaying in the screen in TextArea.
But i am facing the below issues.
The Device back button is allow user to go back. How i can avoid that? ( I have buttons defined in the program dynamically, only that Back button should work )
While shaking the phone or change the position of the phone, then the Random numbers are automatically generating. How to avoid that?. Please advise.
Button Creation Dynamic
final Button buttonToAdd = new Button(this);
buttonToAdd.setText("RandomNumber");
Listener:
buttonToAdd.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Strvalue = (String) buttonToAdd.getText();
if (Strvalue.equals("RandomNumber"))
{
Randomnumbergeneration();
}
}
});
You can overwrite the Activities onBackPressed() method to handle the back-button click event.
#Override
public void onBackPressed() {
// put some code here or just do nothing
// don't call super.onBackPressed() if you want to disable the back function
}
But if you want to publish your application you should follow the official design guidelines and do not disable this behaviour because every android user is used to it and will find it unlikely if the back button does not work anymore.
I am using actionbarsherlock. I have three screens as follows
lists items in list activity
lists items in list activity after an item from first screen is clicked
shows details about the clicked item.
On second screen I want to have home button that takes the user back to first scree, and on third screen I want to have a home button that takes the user back to second screen.
Here is what I'm doing:
//on second activity:
getSupportActionBar().setTitle("First");
getSupportActionBar().setHomeButtonEnabled(true);
//on third activity:
getSupportActionBar().setTitle("Second");
getSupportActionBar().setHomeButtonEnabled(true);
This makes the button on top left show up but nothing happens when I click it.
I found online that this change needs to be made in onOptionsItemSelected as well. But I don't have android.id.home in my code. My IDE throws error on android....
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == /*what should I put here*/) {
finish();
return true;
}
return true;
}
you should put here android.R.id.home - it is an Android home button id
I hawe many view's in my application and now the problem is how to go back from one view to another.
What I could do it by set back Buttons in every view but i would like to use the android back hard button.
I have tried something like this:
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if(keyCode==KeyEvent.KEYCODE_BACK)
{
finish();
}
return false;
}
But the problem is that this will close my application.
Could you please guide me for a proper solution, for example to memorize the last view was set and then to come back to this view or something like this.
Here is the code with which I am changing the view (it's a method in my main activity):
public void CheckIfUserIsLogedIn ()
{
int userPresence = localDatabase.CheckUserPresence();
if (userPresence == 0)
{
setContentView(R.layout.main);
}
else
{
setContentView(R.layout.userlogedon);
}
}
Thank you.
Look!
You are doing this wrong way..
An Activity class should only have on content View. (because it is recommended way and easy to use and implement).
And if you want to go to next View, show it under another separate Activity.
when you will finish it, you will be automatically redirected to previous Activity.
(and you don't need to memorize the Previous View :) )
See here, how to work with Activity Stacks.
I am not sure to understand your problem correctly because Android do all that for you automatically. Once a view is opened when you switch to another view it is paused (on screen but has not focus) or stopped (has no focus)
http://developer.android.com/reference/android/app/Activity.html
If the current view (activity) has been launched by the previous view (activity), pressing the back button will make you "close" the current view and go back to the previous one automatically.
Now two things :
Perhaps your are simply opening all views wihtin the same activity by showing on or off components which is a bad way of doing and is not recommended by android. What you should do is 1 view = 1 activity.
You are thinking like "iPhone/iPad" where you have to implements back buttons in the "views". In android you don't need to do so. Putting the "finish" command in your code at that point seem to close the application which make me think you have programmed as explained in point 1.
Hope it helps
EDIT:
To start a new activity do it like this
startActivity(new Intent(this, MyOtherActivity.class));
you put this in your code where you want to load the new view (activity)
Now if you want to transfer some information between activities you must do something like this :
Intent myIntent; //intent declaration
int aNumber = 10; // info to send to other activity
String aString = "abcd"; // info to send to other activity
// link Intent to the other activity
myIntent = new Intent(view.getContext(), MyOtherActivity.class)
//put the extra info
myIntent.putExtra("myNumber", aNumber);
myIntent.putExtra("myString", aString);
//start the new view/activity
startActivity(myIntent);
and in the new opened activity you retrieve the infos like this (in the oncreate usually)
int aNumber;
String aString;
#Override
public void onCreate(Bundle savedInstanceState) {
aNumber= getIntent().getExtras().getInt("myNumber");
aString= getIntent().getExtras().getString("myString");
}
Actually i m not sure that understand exactly but..
take a map or a shared preference and at the back button set last View on map or Shared preference .
At the calling or at start activity fetch the data which have stored you.
this will helps you.