i want to add screen rotation support to the activity having a pager view. what i want is when user changes screen orientation, the tab of pager view that was open should be opened in the new orientation. but now activity restarts and every time first tab opens. kindly help me . Thanks in Advance.
package com.example.ali.namallibrary;
import android.app.Fragment;
import android.content.Context;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.io.Serializable;
public class aboutLibrary extends AppCompatActivity {
CustomAdapter customAdapterpter = null;
TabLayout tabLayout;
ViewPager viewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about_library);
customAdapterpter = new CustomAdapter(getSupportFragmentManager(),getApplicationContext());
viewPager = (ViewPager) findViewById(R.id.viewPager);
viewPager.setAdapter(customAdapterpter);
tabLayout = (TabLayout) findViewById(R.id.tabBar);
tabLayout.setupWithViewPager(viewPager);
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
});
} // end of oncreate
private class CustomAdapter extends FragmentPagerAdapter {
private String[] fragmentNames = {"About","Collection","Timing","Contact"};
public CustomAdapter(FragmentManager supportFragmentManager, Context applicationContext) {
super(supportFragmentManager);
}
#Override
public android.support.v4.app.Fragment getItem(int position) {
switch (position)
{
case 0 :
return new aboutLibraryFrag();
case 1 :
return new libraryCollectionFrag();
case 2 :
return new libraryTimingFrag();
case 3 :
return new contactUsFrag();
default:
return null;
}
}
#Override
public int getCount() {
return fragmentNames.length;
}
#Override
public CharSequence getPageTitle(int position) {
return fragmentNames[position];
}
}
Whenever your rotate the screen, the activity is completely destroyed and re-created. To deal with this, there is a lifecycle method called onSaveInstanceState. In this method you can save a Bundle object. A Bundle is a bunch of key value pairs that you define, which you want around after the rotation. In that Bundle, you can store the current position of your view pager. I'm haven't worked much with ViewPagers, but getCurrentItem might be what you're looking for. You'd need to make your ViewPager a member variable, and a constant for the key. Then I'm guessing it would look something like:
public void onSaveInstanceState(Bundle savedInstanceState) {
// Save the user's current game state
savedInstanceState.putInt(CURRENT_POSITION_KEY, mViewPager.getCurrentItem());
// Always call the superclass so it can save the view hierarchy state
super.onSaveInstanceState(savedInstanceState);
}
The onCreate method takes a Bundle savedInstanceState - if the activity is being restarted from a rotation, this parameter will have everything you saved in onSaveInstanceState. Thus you can do something like this in onCreate:
// Check whether we're recreating a previously destroyed instance
if (savedInstanceState != null) {
int currentPos = savedInstanceState.getInt(CURRENT_POSITION_KEY);
mViewPager.setCurrentItem(currentPos)
}
More information about loading and saving the state between rotations can be found here: https://developer.android.com/guide/components/activities/activity-lifecycle.html#saras
Another way to deal with screen changes and or rotation is to add android:configChanges to your activity:
<activity android:name="com.example.myactivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize|screenLayout|smallestScreenSize|uiMode" />
The ConfigChanges mentioned will not cause your activity to be detroyed and recreated when they occur instead, Android will simply rotate the screen and then call onConfigurationChanged where you can manually handle the cases yourself.
You can override onConfigurationChanged in your activity to handle the events.
public class myactivity extends Activity
{
//..
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
}
}
Read more in the Android documentation here
Related
My first thread here - sorry if the text formatting is bad.
I use Android Studio 3.1.3 API27 and work on an app for Smartphone.
The app currently consists of 1 activity (split in 3 fragments), a second activity and 5 xml files.
By using a ViewPager, I'm able to swipe through the 3 fragments.
The 2nd fragment (middle fragment) contains 2 buttons that each open the 2nd activity, which contains many color buttons.
When clicking on the color buttons, I can change the background colors of the 1st fragment.
After choosing a color, the 2nd activity gets closed and I'm back in activity 1 -> fragment2.
It works, but the PROBLEM is that I always have to swipe to the 3rd fragment,
then back to the 2nd and then to the 1st.
If I don't do this, the colors of fragment 1 will remain the old ones.
Now I'm looking for a way to update the layout of fragment 1 as soon as I press a color button of activity 2.
I already tried this:
when writing the SharedPreferences (Activity2), I use editor.apply() instead of editor.commit()
when reading the SharedPreferences (Activity1 -> Fragment1), I use Context.MODE_MULTI_PROCESS instead of Context.MODE_PRIVATE
using viewpage.setOffscreenPageLimit(0); in the MainActivity inside of my public void SetUpViewPager(ViewPager viewpage) method.
Nothing helped, though.
This is how it looks like:
MainActivity.java (Activity 1):
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity
{
ViewPager vp;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager vp = findViewById(R.id.vp);
SetUpViewPager(vp);
}
public void SetUpViewPager(ViewPager viewpage)
{
MyViewPagerAdapter Adapter = new MyViewPagerAdapter(getSupportFragmentManager());
Adapter.AddPageFragment(new Page_1(), "Page 1");
Adapter.AddPageFragment(new Page_2(), "Page 2");
Adapter.AddPageFragment(new Page_3(), "Page 3");
viewpage.setOffscreenPageLimit(0);
viewpage.setAdapter(Adapter);
}
public class MyViewPagerAdapter extends FragmentPagerAdapter
{
private List<Fragment> MyFragment = new ArrayList<>();
private List<String> MyPageTitle = new ArrayList<>();
public MyViewPagerAdapter(FragmentManager manager)
{
super(manager);
}
public void AddPageFragment(Fragment Frag, String Title)
{
MyFragment.add(Frag);
MyPageTitle.add(Title);
}
#Override
public Fragment getItem(int i)
{
return MyFragment.get(i);
}
#Nullable
#Override
public CharSequence getPageTitle(int position)
{
return MyPageTitle.get(position);
}
#Override
public int getCount()
{
return 3;
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.menu_main, 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);
}
}
Page_1.java (Activity 1 -> Fragment 1):
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import static android.content.Context.MODE_PRIVATE;
public class Page_1 extends Fragment
{
int backgroundColorLeft, backgroundColorRight, textColorLeft, textColorRight; // Variables for SharedPreferences
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View PageOne = inflater.inflate(R.layout.page1, container, false); // Link view to layout?
return PageOne;
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState)
{
SharedPreferences prefs = getActivity().getSharedPreferences("bgColor", Context.MODE_MULTI_PROCESS); // Load saved shared file
backgroundColorLeft = prefs.getInt("backgroundColorLeft", backgroundColorLeft); // Load saved background color for left layout
textColorLeft = prefs.getInt("textColorLeft", textColorLeft); // Load saved text color for left layout
backgroundColorRight = prefs.getInt("backgroundColorRight", backgroundColorRight); // Load saved background color for right layout
textColorRight = prefs.getInt("textColorRight", textColorRight); // Load saved text color for right layout
RelativeLayout relLayoutLeft = getActivity().findViewById(R.id.rel_layout_left); // Link variable to ID of left layout
relLayoutLeft.setBackgroundColor(backgroundColorLeft); // Change background color of left layout
TextView tvLeft = getActivity().findViewById(R.id.tv_left); // Link variable to ID
tvLeft.setTextColor(textColorLeft); // Change text color of left layout
RelativeLayout relLayoutRight = getActivity().findViewById(R.id.rel_layout_right); // Link variable to ID of right layout
relLayoutRight.setBackgroundColor(backgroundColorRight); // Change background color of right layout
TextView tvRight = getActivity().findViewById(R.id.tv_right); // Link variable to ID
tvRight.setTextColor(textColorRight); // Change text color of right layout
super.onActivityCreated(savedInstanceState);
}
}
Page_2.java (Activity 1 -> Fragment 2):
package com.example.konstantin.clipcodes_swiping;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import static android.content.Context.MODE_PRIVATE;
public class Page_2 extends Fragment
{
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View PageTwo = inflater.inflate(R.layout.page2, container, false);
Button buttonLeft = PageTwo.findViewById(R.id.button_left); // Link variable to ID of left button
buttonLeft.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
int pos = 1; // Set position to left
setPosition(pos); // Load setColor method and send 2 color values
}
});
Button buttonRight = PageTwo.findViewById(R.id.button_right); // Link variable to ID of right button
buttonRight.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
int pos = 2; // Set position to right
setPosition(pos); // Load setColor method and send 2 color values
}
});
return PageTwo;
}
public void setPosition (int pos) // Start second activity to choose colors
{
Intent intentPos = new Intent(getActivity(), Page_4_Colors.class); // Create intent for current Activity and target activity
SharedPreferences prefs = getActivity().getSharedPreferences("bgColor", Context.MODE_MULTI_PROCESS); // Create new SharedPreferences instance
SharedPreferences.Editor editor = prefs.edit(); // Assign variable to editor function
editor.putInt("position", pos); // Write selected position (int) inside of editor
editor.apply(); // Save values, close process
getActivity().startActivity(intentPos); // Start second activity
}
}
Page_3.java (Activity 1 -> Fragment 3):
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Page_3 extends Fragment
{
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View PageThree = inflater.inflate(R.layout.page3, container, false);
return PageThree;
}
}
Page_4_Colors.java (Activity 2):
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class Page_4_Colors extends Activity
{
int pos;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.page4_colors);
SharedPreferences prefs = getSharedPreferences("bgColor", MODE_MULTI_PROCESS); // Load saved shared file
pos = prefs.getInt("position", pos); // Load saved position (int)
Log.wtf("Position", String.valueOf(pos)); // Show pos value in Log
Button buttonWhite = findViewById(R.id.button_white);
buttonWhite.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
setColor(getResources().getColor(R.color.white), getResources().getColor(R.color.black)); // Load setColor method and send 2 color values
}
});
Button buttonYellow = findViewById(R.id.button_yellow);
buttonYellow.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
setColor(getResources().getColor(R.color.yellow), getResources().getColor(R.color.black)); // Load setColor method and send 2 color values
}
});
Button buttonOrange = findViewById(R.id.button_orange);
buttonOrange.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
setColor(getResources().getColor(R.color.orange), getResources().getColor(R.color.black)); // Load setColor method and send 2 color values
}
});
Button buttonRed = findViewById(R.id.button_red);
buttonRed.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
setColor(getResources().getColor(R.color.red), getResources().getColor(R.color.black)); // Load setColor method and send 2 color values
}
});
Button buttonGreen = findViewById(R.id.button_green);
buttonGreen.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
setColor(getResources().getColor(R.color.green), getResources().getColor(R.color.black)); // Load setColor method and send 2 color values
}
});
Button buttonBlue = findViewById(R.id.button_blue);
buttonBlue.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
setColor(getResources().getColor(R.color.blue), getResources().getColor(R.color.white)); // Load setColor method and send 2 color values
}
});
}
public void setColor (int backgroundColor, int textColor) // Write color values into SharedPreferences
{
SharedPreferences prefs = getSharedPreferences("bgColor", MODE_MULTI_PROCESS); // Create new SharedPreferences instance
SharedPreferences.Editor editor = prefs.edit(); // Assign variable to editor function
if (pos == 1)
{
editor.putInt("backgroundColorLeft", backgroundColor); // Write background color (int) inside of editor
editor.putInt("textColorLeft", textColor); // Write text color (int) inside of editor
}
if (pos == 2)
{
editor.putInt("backgroundColorRight", backgroundColor); // Write background color (int) inside of editor
editor.putInt("textColorRight", textColor); // Write text color (int) inside of editor
}
editor.apply(); // Save values, close process
this.finish(); // Close this activity
}
}
Thanks for any help!
use EventBus
unregister and register EventBus in Page_1 onStop() and onStart()
EventBus.getDefault().unregister(this)
EventBus.getDefault().register(this)
and use this for post the value
EventBus.getDefault().post(new MessageEvent("Change Color"));
and this function will handle the MessageEvent
#Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(MessageEvent event) {
//change the color here
//add this function in Page_1
}
when you update the value of color. put in MessageEvent documentation
You can update the UI (or at least the color related part) for each fragment in the onResume() method, thus, when you return from the second activity, it will refresh.
When a Fragment is made visible (i.e., the selected page in your ViewPager), its setUserVisibleHint() method is called. You can override that method in your Fragment and use it to trigger a refresh.
#Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if(isVisibleToUser){
//you can check if the color is changed then refresh the fragment if not then don't do anything
//here you should refresh your fragment , this will called every time you
//view this fragment in all cases even if you didn't move to the
//third tab
}
}
How To Refresh A Fragment
Fragment currentFragment = getFragmentManager().findFragmentByTag("YourFragmentTag");
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.detach(currentFragment);
fragmentTransaction.attach(currentFragment);
fragmentTransaction.commit();
I have implemented the settings screen of my app using PreferenceFragment.
When the up button in the toolbar of the settings screen is clicked my app returns to the MainActivity screen but the data that was previously entered into the EditTexts (before going to the settings screen) is lost and the EditTexts are all blank.
I tried implementing onSaveInstanceState and onRestoreInstanceState but it is not working because onRestoreInstanceState is not called when clicking the up button in the settings screen toolbar. The savedInstanceState is also null in onCreate.
Would really appreciate it if someone could point out how to go about restoring the data in the EditTexts please? :)
Here's the log for MainActivity:
Clicking on Settings from toolbar menu:
I/LOG: onPause
I/LOG: onSaveInstanceState saving
I/LOG: onStop
Clicking on back button in settings screen toolbar:
I/LOG: onDestroy
I/LOG: onCreate
I/LOG: savedInstanceState is null
I/LOG: onStart
I/LOG: onResume
MainAcitivy.java
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;
import android.util.Log;
//...
public class MainActivity extends AppCompatActivity {
Toolbar toolbarMain;
EditText editText;
#Override
protected void onCreate(Bundle savedInstanceState) {
Log.i("LOG", "onCreate");
super.onCreate(savedInstanceState);
Log.i("LOG", "savedInstanceState is " + savedInstanceState);
if(savedInstanceState !=null) {
editText.setText(savedInstanceState.getString("ET_KEY"), TextView.BufferType.EDITABLE);
}
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.myEditText);
toolbarMain = (Toolbar) findViewById(R.id.toolbar);
toolbarMain.setTitle(R.string.app_name);
toolbarMain.inflateMenu(R.menu.menu_main);
toolbarMain.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.settings:
startActivity(new Intent(MainActivity.this, SettingsActivity.class));
}
return true;
}
});
}
#Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
Log.i("LOG", "onRestoreInstanceState");
super.onRestoreInstanceState(savedInstanceState);
editText.setText(savedInstanceState.getString("ET_KEY"), TextView.BufferType.EDITABLE);
}
#Override
public void onSaveInstanceState(Bundle outState) {
Log.i("LOG", "onSaveInstanceState saving");
outState.putString("ET_KEY", editText.getText().toString());
}
#Override
public void onPause() {
Log.i("LOG", "onPause");
super.onPause();
}
#Override
public void onStop() {
Log.i("LOG", "onStop");
super.onStop();
}
#Override
public void onDestroy() {
Log.i("LOG", "onDestroy");
super.onDestroy();
}
#Override
public void onStart() {
Log.i("LOG", "onStart");
super.onStart();
}
#Override
public void onResume() {
Log.i("LOG", "onResume");
super.onResume();
}
SettingsActivity.java
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
public class SettingsActivity extends AppCompatActivity {
SharedPreferences sharedPref;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.preferences_layout);
sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// Display the fragment as the main content.
getFragmentManager().beginTransaction()
.replace(R.id.pref_content, new SettingsFragment())
.commit();
}
}
I found a solution after much searching. Hope the following helps anyone else out there.
Initially, I tried to override the behaviour of the up button. However I was getting a 'cannot resolve symbol' error on MenuItem when trying to override onOptionsItemSelected. Perhaps there is a way to do this but I couldn't figure it out. I gave up on this approach when I noticed this in the Android Developer Docs:
You do not need to catch the up action in the activity's onOptionsItemSelected() method. Instead, that method should call its superclass, as shown in Respond to Actions. The superclass method responds to the Up selection by navigating to the parent activity, as specified in the app manifest.
Solution
A solution that did work for me was to create a custom toolbar so that I could define the behaviour of the up button entirely. Thankfully this only required a few lines of code.
Step 1 - Create a new toolbar layout
res/layout/toolbar_preferences.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/toolbarPreferences"
android:background="#color/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:elevation="4dp"
android:theme="#style/ToolbarTheme"
app:navigationIcon="?attr/homeAsUpIndicator"
app:titleTextColor="#color/white">
</android.support.v7.widget.Toolbar>
Note the line in the above:
app:navigationIcon="?attr/homeAsUpIndicator"
Step 2 - Refer to new toolbar in preferences layout file
In my case, in my preferences_layout.xml:
<include layout="#layout/toolbar_preferences" />
Step 3 - Java code
In SettingsActivity.java, I had to:
First, initiate the new toolbar:
Toolbar toolbarPref = (Toolbar) findViewById(R.id.toolbarPreferences);
setSupportActionBar(toolbarPref);
Second, set click listener and define bahaviour:
toolbarPref.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
Done.
I know this question is asked often but none of the accepted solutions have worked for me. (reference solution)
I'm getting the classic "Wrong 2nd argument type" error when I try to launch a Preference Menu fragment from within one of my otherfragments.
Unforunately, even after doing what the accepted answer in that question suggests (calling FragmentManager fragmentManager = getSupportFragmentManager() and importing android.support.v4.app.FragmentManager; I still see the "Wrong 2nd argument type" bug when I call .replace().
A quick overview: my MainActivity.java opens a fragment viewpager (MenuPager.java), from which I try to launch another fragment (FragmentSettingsMenu.java) when a button is clicked within FragmentTrackRecordMenu.java (a child fragment of MenuPager.java).
My code is below. I try to launch the settings menu fragment when the menu button (within FragmentTrackRecord) is clicked but I can't get around the aforementioned compiler error. How can I display my PreferencesFragment successfully from this onClick?
MainActivity.java
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
public class MainActivity extends FragmentActivity {
public static ViewPager menuPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
...
menuPager = (ViewPager) findViewById(R.id.pager);
PagerAdapter pageAdapter = new MenuPagerAdapter(getSupportFragmentManager());
menuPager.setAdapter(pageAdapter);
...
}
}
MenuPagerAdapter.java
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class MenuPagerAdapter extends FragmentPagerAdapter {
public MenuPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
switch (arg0) {
case 0:
return new FragmentNeckDisplayMenu();
case 1:
return new FragmentCapoMenu();
case 2:
return new FragmentTrackRecordMenu();
default:
break;
}
return null;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return 3;
}
}
FragmentTrackRecordMenu.java
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
public class FragmentTrackRecordMenu extends Fragment {
private Button menuIcon;
public FragmentTrackRecordMenu(){ } //default constructor
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.menu_fragment_recorder, container, false);
menuIcon = (Button) v.findViewById(R.id.menuIcon);
menuIcon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) { //open popup window
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(android.R.id.content, new FragmentSettingsMenu()) //COMPILER ERROR
.commit();
}
});
}
}
FragmentSettingsMenu.java
public class FragmentSettingsMenu extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from the XML resource
addPreferencesFromResource(R.xml.preferences);
}
}
getSupportFragmentManager() returns the fragment manager of android.support.v4.app.FragmentManager and your FragmentSettingMenu.java is of android.app.Fragment. That's why "Wrong 2nd argument type" error because they are incompatible.
your FragmentManager is of v4 type and FragmentSettingsMenu is not a v4 type.Hence it does not allow.Try the below line in activity.
getFragmentManager().beginTransaction().replace(android.R.id.content,
new PrefsFragment()).commit();
.try this link here
it may give you some help.
I have the following code :
MainActivity.java
package com.erc.library;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Environment;
import android.os.StrictMode;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;
import com.erc.sayeghlibrary.adapter.TabsPagerAdapter;
public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Stories", "Dictionaries", "eBooks"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int actionBarTitleId = Resources.getSystem().getIdentifier("action_bar_title", "id", "android");
if (actionBarTitleId > 0) {
TextView title = (TextView) findViewById(actionBarTitleId);
if (title != null) {
title.setTextColor(Color.WHITE);
}
}
// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
/**
* on swiping the viewpager make respective tab selected
* */
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// on tab selected
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// action with ID action_refresh was selected
case R.id.Favorites:
Toast.makeText(this, "Favorites selected", Toast.LENGTH_SHORT)
.show();
break;
// action with ID action_settings was selected
default:
break;
}
return true;
}
}
TabsPagerAdapter.java
package com.erc.library.adapter;
import com.erc.library.Dictionaries;
import com.erc.library.Ebooks;
import com.erc.library.Stories;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class TabsPagerAdapter extends FragmentPagerAdapter {
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new Stories();
case 1:
return new Dictionaries();
case 2:
// Movies fragment activity
return new Ebooks();
}
return null;
}
#Override
public int getCount() {
// get item count - equal to number of tabs
return 3;
}
}
I'm making a library application in which the navigation is by tabs, the problem is each time I go from the third tab to the first or the first to the third, the tab content is refreshing, I want to prevent the refresh, Any help please ?
By default, ViewPager recreates the fragments when you swipe the page. To prevent this, you can try one of three things:
1. In the onCreate() of your fragments, call setRetainInstance(true).
2. If the number of fragments is fixed & relatively small, then in your onCreate() add the following code:
mViewPager = (ViewPager)findViewById(R.id.pager);
mViewPager.setOffscreenPageLimit(limit); /* limit is a fixed integer*/
3. Use a FragmentPagerAdapter as part of your ViewPager.
If I remember correctly, the second option is more promising. But I urge you to try all three and see which of them work.
one has to instance the FragmentPagerAdapter first, then .getCount() will return a value -
while .getCount() - 1 should be set as the default off-screen limit:
TabsPagerAdapter adapter = new TabsPagerAdapter(getSupportFragmentManager());
/* the ViewPager requires a minimum of 1 as OffscreenPageLimit */
int limit = (adapter.getCount() > 1 ? adapter.getCount() - 1 : 1);
ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(adapter);
viewPager.setOffscreenPageLimit(limit);
you can handle view recreation by check if the view is null or not
public class FragmentExample extends Fragment {
private View rootView;
public FragmentExample() {}
#Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
if (rootView == null) {
rootView = inflater.inflate(R.layout.fragment_example_layout, container, false);
// Initialise your layout here
} else {
((ViewGroup) rootView.getParent()).removeView(rootView);
}
return rootView;
}
}
In the onCreate() of your fragments, call setRetainInstance(true)
A bit late to this question, but thanks to Y.S., got to know how ViewPager works. I was building an app with 4 tabs and at any point of time, noticed that only two tabs were being refreshed, which I suppose was a default behaviour. After hours of investigation, I understood that Android refreshes multiple tabs to bring in a smooth swiping performance for the user - you might notice that you would have clicked tab2, but android brings in the data for tab3 and keeps it ready.
Though this behaviour is good, it has its pros and cons. Pros - you get a smooth swiping experience, without data being loaded from an external server when you land up in that tab. Cons-your backstack implementation in tabs could go for a toss. When you click a tab, the view pager actually calls smoother tab and you'll end up in a big trouble, if your methods are setting up backarrow (home) at the top left based on what is in the backstack in the clicked tab.
setOffscreenPageLimit is the answer to this. If you want your custom backstack framework to function, and do not want tab3 to be called when tab2 is clicked, you simply need to set the value to the number of tabs. For instance, if you have 4 tabs, set setOffScreePageLimit(4). This would mean that Android would refresh all the 4 fragments initially, which is a bit of a performance overhead (which you should manage properly). But, your backstack and tab switching remain intact. Hope this helps.
Since the activity implements ActionBar.TabListener, the activity's onCreate() is getting called again and again. So place the following code in onResume() method:
// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
/**
* on swiping the viewpager make respective tab selected
* */
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
In my case above suggestion does not work.
To restrict recreation of fragment, what i did:
In onCreateView you can store inflated view in a global variable and initialize it only if it is null, like in this code:
var root:View?=null
var apiDataReceived=false
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
if (root==null)
root=inflater!!.inflate(R.layout.fragment_layout, container, false)
return root
}
Now if you are parsing some data and fill it into RecyclerView or any other View
Make a global variable like in above code apiDataReceived
Set it to true if you successfully parsed data.
Before apiCalls place a condition like this:
if (!apiDataReceived) {
apiCalls()
}
So if apiCalls() would be called only if data is not parsed.
Do your http calls and parsing or any other thing in method which called after onCreateView like onStart
The above code is in kotlin, If you are facing any issue, let me know in comments.
I am very new to Android development. I am following Google's Android "classes" and am receiving an error for this code in Eclipse:
package com.feistie.myfirstapp;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize member TextView so we can manipulate it later
mTextView = (TextView) findViewById(R.id.text_message);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
// For the main activity, make sure the app icon in the action bar
//does not behave as a buutton
ActionBar actionBar = getActionBar();
actionBar.setHomeButtonEnabled(false);
}
}
#Override
public void onDestroy() {
super.onDestroy(); // Always call the superclass
// Stop method tracing that the activity started during onCreate()
android.os.Debug.stopMethodTracing();
}
#Override
public void onPause() {
super.onPause(); // Always call the superclass method first
// Release the Camera because we don't need it when paused
// and other activities might need to use it.
if (mCamera != null) {
mCamera.release();
mCamera = null;
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
/** Called when the user clicks the Send button */
public void sendMessage (View view) {
// Do Something in response to button
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
There is an error for each of these lines:
if (mCamera != null) {
mCamera.release();
mCamera = null;
}
The error for the first and third lines says "mCamera cannot be resolved to a variable." The error for the second line just says "mCamera cannot be resolved."
If you need more information please let me know.
Thanks!
You need to declare mCamera before you can reference it:
public class MainActivity extends Activity {
Camera mCamera;
And then you need to initialize it, probably in onResume()
#Override
protected void onResume() {
super.onResume();
mCamera = Camera.open()
}
Make sure you added the appropriate permission that you manifest:
<uses-permission android:name="android.permission.CAMERA" />
Addition
You need to declare_every_ variable before you try use it in Java. I don't see where you declare mTextView either.
public class MainActivity extends Activity {
Camera mCamera;
TextView mTextView;