Is there a way to say if an activity is selected then do something?
I dont know the statement which is why i am asking this question, but similar to this:
if(myactivity.isselected(true)){
webview3.reload();}
If you are using tabs, then onTabChange() is the correct event for that. Not all tabs are individual activities. Not even necessarily different layouts. Then you can check to see if the selected tab is the activity like this
public void onTabChange(View v, int id) {
switch (v.getId()) {
case (R.id.tab1) {
//Do this
break;
}
}
The tabhost supports alot of functionality. Another funcitonis getTab() which will return the id (as an int) of the tab selected. Heres a link to the documentation http://developer.android.com/reference/android/widget/TabHost.html
When user moves through different user interface , he/she start different activities. Since you have different tabs, you should have three different screens and so three different activities , if user is moving from one tab to another, you should first pause the initial activity and should start other activity.
You can get the activities that the tabhost is running, with this:
YourActivity act = (YourActivity) getLocalActivityManager().getActivity(TABKEY_1);
then call any method you have in that activity. TABKEY_1 is just the name for the tab that you give when you initialized the tabhost.
Related
I want to toggle a SwitchPreference from another activity (Main Activity).
I have an activity where I ask the user if they want to enable notifications. It contains two buttons and they either click yes or no, as shown below:
I want the answer to be saved and the SwitchPreference to either be toggled, On for yes or off for no. (as well as the SwitchPreference state)
I'm really stuck on how to do this.
Any advice or answers would be most appreciated?
You've got a couple options.
You could do as #0X0nosugar suggested where you simply store the new setting value in SharedPreferences and check the value when resuming the previous activity.
You could use startActivityForResult(intent, SOME_CONSTANT_IDENTIFIER) (vs just startActivity(intent)) in order to have your settings activity return a result to your calling activity. The concept here is that you're starting a new activity which will return a result back to the previous activity. You could then check the value, store it where needed, and update any relevant UI items. Here's an article on how this works https://developer.android.com/training/basics/intents/result.
I have two activtiy. When a boolean variable in activity 1 is true then the UI in activity 2 should be update (activity 2 is with fragment). When I back to activity 2 with (setDisplayHomeAsUpEnabled) it works correctly but with back button it does not work. What is the difference between this ways and how I can solve this. I try update the activity 2 with this code but it does not work:
#Override
public void onBackPressed() {
super.onBackPressed();
//update();
}
The Home Button is not meant to behave like the Back Button. See this reference. The Home or "Up" Button should take the user up within the view hierarchy (i.e. if you have a number of multiple choice fragments, don't go back through each of them, but back to the activity that started the first fragment)
Each activity should specify what it's parent activity is in order to properly use this functionality. You can do this through the manifest, or by overriding onOptionsItemSelected() as outlined here
In terms of why your activity 2 may be updating when using the "Up" button but not the back button, this could be because both handle the back stack in a different way. Back will take you to the last thing on the stack and resume it, assuming you don't haven't tagged the activity with a launchmode that alters this behavior. Here's more info on the back stack. If you haven't designated your Activity 2 as "singletop" then when you use the "Up" Button, a new instance of that parent activity is created. See here. This may means that the information is updating after using the home button because it's creating a new instance of the activity, while for the back button - you are not creating a new instance but resuming a previous instance... make sure you implement an onResume() function to properly handle this and update the information.
TL,DR: How to start from "Activity A" both the "Activity B" and the fragments that will be shown in it.
I'm new to Android Studio so I'm clueless about most things concerning fragments. I'm working on an app that calculates information about different shapes. At first the user can pick a shape, then they are taken to an activity where they input the dimensions. What I'm trying to do is, instead of creating an activity for every shape, I created a single activity in which I'd like to start different fragments. I created a single fragment for every dimension (say side a, side b and so on...).
It's a bit hard to explain for me but what I'm trying to do is, from the first activity - "Menu.java" through a button open up a second activity- "Input.java" activity (containing just a confirm button) along with some fragments as soon as the "Input" activity starts (depending on the shape the fragments may vary, whether just in position or over all in which are loaded and which not - the reason i think i need to do it like this).
To simplify (and hopefully make it a bit more understandable) - from "Activity A" start both the "Activity B" and the fragments that will be shown in it.
I don't have any code besides the fragments themselves and the buttons set up because like I said I'm mostly clueless and I'm thankful for every advice or at least clarification that it isn't possible. Hovewer if there is any other information that can help or you want me to try to explain it a bit better just make a comment and I'll and what you need.
Thanks a lot
--EDIT--
here is all the code I have ready:
public class Menu extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
}
public void inputSquare(View view){
Intent intent = new Intent(this, Input.class);
//Here is where I'd like to start let's say fragment "Input_A_Fragment" in the Input class (or even if this isn't the way/place to do it please le me know)
startActivity(intent);
}
}
I have to implement a navigation system similar to the one used in the Instagram Android client.
There should be a permanent tabbar on the bottom of the screen all the time.
When the user navigates deeper within one of these tabs, lets say to a detail view, then switches to another tab, and then switches back to the previous tab, the last shown (deeper) detail view should be shown, and on back presses, it should be iterating back till the main view of the said tab.
What I have came up with so far is the following:
I have a MainAcitvity showing the menu on the bottom.
On selecting a menu point, the appropriet Fragment is shown.
When the user navigates further within a Fragment, it then asks the MainActivity to change its content by the given criterias, resulting in changing the Fragment shown.
I add all the Fragment changes to the backStack, by calling the FragmentTransaction's addToBackStack() method.
I am stuck at this point, and cannot figure out how to switch fragments on back presses, and how to handle tab navigations when deeper views are shown instead the main views of the tabs.
I am thinking of using my own separate "backstack implementations" for every tab. When the user navigates deeper within a tab, i generate a unique "tag" and use that tag when calling addToBackStack() and also putting the tag in the "backStack" implemented by me. In case the user navigates again to this tab, i can check if i have any tags in the "backStack" for that tab, and if so, then look up that entry in the real backStack in the fragmentManager of the MainActivity, and switch to it.
I could not come up with anything better yet. Is there any better/simpler way to attchieve the said behaviour? Am i missing something? (I know this is really bad application design in the Android world, but it is another question)
I am posting an answer since the question is pretty dead, yet the conclusion might be helpful for others.
We ended up sticking with the old fashioned NavgationDrawer pattern, which worked well. But in the meantime I had to implement a library project which provided a Fragment to the hosting Application which had its own custom logic. This Fragment then used its ChildFragmentManager, to add another Fragments inside itself. ChildFragmentManager is ported back in the Android Support v4 lib, so you can use it basicly everywhere.
So lets say you want x Menu points in which you can navigate deeper. Those will be the Fragments using their own ChildFragmentManagers to add other Fragments to navigate deeper within that Menu. ChildFragmentManagers have their own back stack, so you dont have to worry that much about handling states. If another Menu is selected, you can look up the corresponting Fragment in the MainActivitys FragmentManager, and change back to it, or add it if it has not yet been added.
Be careful, you have to implement back functionality yourself, since ChildFragmentManagers will not get the backPressed event automatically. You can do this by handling the onBackPressed event in your MainActivity.
#Override
public void onBackPressed() {
boolean handled = false;
if(getFragmentManager().getBackStackEntryCount() == 0){
// No menu added
return super.onBackPressed();
}
Fragment frag = getFragmentManager().getBackStackEntryAt(getFragmentManager().getBackStackEntryCount() - 1);
if (frag instanceof XMenuFragment && frag.isVisible()) {
FragmentManager childFm = frag.getChildFragmentManager();
if (childFm.getBackStackEntryCount() > 0) {
// pop the last menu sub-Fragment
childFm.popBackStack();
handled = true
}
}
if(!handled){
super.onBackPressed();
}
}
I used a different code, so it might contain errors, but i hope that the point of the concept is clear.
I am executing maswebview class and I would like to finish only this activity. I tried maswebview.this.finish() but when executed, app is been closed. Then if I set a new view for the tab content, it is loaded properly and webviewmas dissapears but just for a while, then appears again fitting fullscreen. How to finish maswebview completely? ThanK you
public void onClick(View arg0)
{
/*
Intent intent = getIntent();
intent.addFlags(Intent.FLAG_ACTIVITY_TASK_ON_HOME);
startActivityForResult(intent, 1);
Intent intentmas = new Intent (maswebview.this, mas.class);
intentmas.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intentmas.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intentmas.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intentmas.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
View vista = getLocalActivityManager().startActivity("maswb", intentmas).getDecorView();
setContentView(vista); */
maswebview.this.finish();
Do you have any other activities of your app in the stack by the time you call finish()? If you don't, you'll want to start the desired activity instead of finishing the current one.
But actually it seems to me that you're trying to accomplish something that can be done simpler. Can you provide more info on the task at hand and your app structure you're trying to go about it with?
From what you said, it seems like you have tabbed UI and you're trying to show a webview in one of the tabs, then hide it.
First, I don't see why you want the webview in a separate activity. Seems to me you could just have it in layout of one of the tabs and just call setVisibility(GONE) to hide it.
Second - and this is important - looks like you're trying to implement tabs the old way - TabWidget, TabHost, etc. Since Honeycomb has been released, there's much more convenient way to implement tabbed UI - Action Bar Tabs and Fragments - that approach is much more convenient and will render your webview problems obsolete: there's a thing called WebViewFragment which is basically a WebView but smarter - it will handle its own lifecycle with minimum effort required from you (i.e. pause when removed, resume when added). It will take some effort to study up on Fragments, but it's well worth it. You can get Fragments API for pre-Honeycomb android sdks as a static library - it's called android-support-v4 and you can download it in Android SDK Manager.
Are you calling "maswebview.this.finish();" before the new Activity is started?
Also if you want to just start this new activity without having the old activity in existence then you can add android:nohistory="true" to your manifest.xml. This will cause the current activity to be the only activity in the queue.
See HERE
You need to provide a little more information for us to better understand what exactly is going on.
I hope this helps.
Try following code
#Override
public void onBackPressed() {
if(childView != null && parentLayout.getChildCount()==2){
childView.stopLoading();
parentLayout.removeViewAt(parentLayout.getChildCount()-1);
if(webView.getVisibility() == View.GONE)
webView.setVisibility(View.VISIBLE);
}else{
super.onBackPressed();
}
Do you have a parent activity which is launching this one? If yes make sure you don't call finish() after launching maswebview, that way the parent activity will remain in the stack. Once maswebview would exit, onResume would be called for your parent activity.