Hi I am working on sliding fragments when user tap on button are draw the view. By using this library SlidingMenu library. This library is working perfectly.But I am facing a problem in Fragment class when I am sliding a view in my fragmentactivity in fragment class there is no methods are calling What I am trying here is my code :
FragmentActivity: this activity extends SlidingFragmentActivity(This activity is in side abvoe mentioned library project)
#Override
#SuppressLint("NewApi")
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
View view1 = getLayoutInflater().inflate(
R.layout.activity_main, null);
View view2 = getLayoutInflater().inflate(R.layout.menu_frame, null);
View view3 = getLayoutInflater().inflate(R.layout.menu_frame_two, null);
getSlidingMenu().setMode(SlidingMenu.LEFT_RIGHT);
getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
getSlidingMenu().setMenu(view2);
getSupportFragmentManager().beginTransaction()
.replace(R.id.menu_frame, new Fragment1()).commit();
getSlidingMenu().setSecondaryMenu(view3);
getSupportFragmentManager().beginTransaction()
.replace(R.id.menu_frame_two, new Fragment2()).commit();
setContentView(view1);
}
Fragment1.java this activity extends Fragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
System.out.println("oncreate view");
View v = inflater.inflate(R.layout.my_fragment_layout, container, false);
return v;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
System.out.println("on activity created");
}
#Override
public void onResume() {
// TODO Auto-generated method stub
super.onResume();
System.out.println("onResume");
}
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
System.out.println("oncreate");
}
#Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity);
System.out.println("onAttach");
}
#Override
public void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
super.onSaveInstanceState(outState);
System.out.println("on save instance state");
}
#Override
public void onHiddenChanged(boolean hidden) {
// TODO Auto-generated method stub
super.onHiddenChanged(hidden);
System.out.println("onHiddenChanged");
}
#Override
public void onStart() {
// TODO Auto-generated method stub
super.onStart();
System.out.println("onStart");
}
#Override
public void onPause() {
// TODO Auto-generated method stub
super.onPause();
System.out.println("onPause");
}
#Override
public void onStop() {
// TODO Auto-generated method stub
super.onStop();
System.out.println("onStop");
}
#Override
public void onViewStateRestored(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onViewStateRestored(savedInstanceState);
System.out.println("onViewstaterestored");
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onViewCreated(view, savedInstanceState);
System.out.println("onViewCreated");
}
When I am slide a view left are right side the above methods are not calling. But when I am click on menu button oncreateview is calling.
Inside on click I am calling getSlidingMenu().showMenu(); this method.
But i want to call any method when I slide on view. What was the problem in my code?
why any method is not calling inside of fragment when sliding?
Any one please help me..Thanks in advance.
Refer this tutorial. I think it's helpful for you.
Implementing Horizontal View Swiping Using ViewPager and FragmentPagerAdapter in Android
Related
Ok, this one is really tricky to me. The idea of this is a time log for EMS. You press a button by the action taken, and it logs the time for later use. I managed to figure out how to save the TextView into a string. Now, how on earth do I save the logTime1.setEnabled(false); so that on rotation or on leaving the activity, it restores it disabled? Eventually I will have another button beside it that will allow you to edit which will unlock the button. Here is the code.
public class TimeLog extends Activity{
boolean logTimeDis1=true;
Button logTime1;
String time1;
TextView ivTimeStamp;
int counter=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.time_log);
logTime1 = (Button) findViewById(R.id.logTime1);
ivTimeStamp = (TextView) findViewById(R.id.ivTimeStamp);
if(savedInstanceState != null) logTime1.setEnabled(savedInstanceState.getBoolean("logTimeDis1", true));
logTime1.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
time1 = new SimpleDateFormat("HH:mm", Locale.US).format(new Date());
ivTimeStamp.setText(time1);
logTime1.setEnabled(false);
logTimeDis1 = false;
return false;
}
});
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
counter++;
}
#Override
protected void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
super.onSaveInstanceState(outState);
outState.putString("ivTimeStamp", ivTimeStamp.getText().toString());
outState.putBoolean("logTimeDis1", logTimeDis1);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onRestoreInstanceState(savedInstanceState);
ivTimeStamp.setText(savedInstanceState.getString("ivTimeStamp"));
}
}
So, in this line
if(savedInstanceState != null) logTime1.setEnabled(savedInstanceState.getBoolean("logTmeDis1", false));
you don't check if savedInstanceState contains logTmeDis1. Say, savedInstanceState is not null, but does not have logTmeDis1, you'll get false and your button locked. Actually, you don't have to check it in this case, just change false to true, and it should work correctly.
Guys I am stuck in a problem with these two methods:-
When I change the orientation of device and set the text in edit text after retriving the text from bundle,it does't work.But the same code is working in onrestoreStoreInstante method.
Please have a look at my code:-
public class LifeCycleActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
EditText user;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
user=(EditText)findViewById(R.id.et_user);
if(savedInstanceState!=null){
String s =savedInstanceState.get("Key").toString();
user=(EditText)findViewById(R.id.et_user);
user.setText(savedInstanceState.get("Key").toString());
Toast.makeText(this, s,Toast.LENGTH_SHORT).show();
}
Toast.makeText(this, "onCreate",Toast.LENGTH_SHORT).show();
Button b=(Button)findViewById(R.id.button1);
b.setOnClickListener(this);
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Toast.makeText(this, "onSaveInstanceState",Toast.LENGTH_SHORT).show();
outState.putString("Key", "Deepak");
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
//String s =savedInstanceState.get("Key").toString();
//user.setText(s);
Toast.makeText(this, "onRestoreInstanceState",Toast.LENGTH_SHORT).show();
}
#Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
Toast.makeText(this, "onStart",Toast.LENGTH_SHORT).show();
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Toast.makeText(this, "onResume",Toast.LENGTH_SHORT).show();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
Toast.makeText(this, "onPause",Toast.LENGTH_SHORT).show();
}
#Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
Toast.makeText(this, "onStop",Toast.LENGTH_SHORT).show();
}
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Toast.makeText(this, "onDEstroy",Toast.LENGTH_SHORT).show();
}
#Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
Toast.makeText(this, "onRestart",Toast.LENGTH_SHORT).show();
}
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
startActivity(new Intent(LifeCycleActivity.this,SecondActivity.class));
}
}
when I set the text in edit text in oncreate method after getting the value from Bundle,it doesn't work.But the same code works in onRestoreInstanceState() method.
According to me , it should work for oncreate also as we can get the Bundle class object there.
Please help me to sort out this problem..
EditText and most of other views have their own methods to save/restore their own data. So in general it's not necessary to save/restore them on your code.
You can see this here on the Android TextView source code (remember that EditText extends from TextView) on line 3546:
if (ss.text != null) {
setText(ss.text);
}
so the reason you can't set it onCreate and can do it onRestoreInstanceState it's because during your activity super.onRestoreInstanceState(savedInstanceState) the activity calls the EditText.onRestoreInstanceState and the EditText restore it self to the previous value.
You can see it happening on the Activity source code on line 940
protected void onRestoreInstanceState(Bundle savedInstanceState) {
if (mWindow != null) {
Bundle windowState = savedInstanceState.getBundle(WINDOW_HIERARCHY_TAG);
if (windowState != null) {
mWindow.restoreHierarchyState(windowState);
}
}
}
hope it helps.
Overview
onSaveInstanceState() is used to put the data to bundle
onRestoreInstanceState() is used to set the data available in
bundle to your activity
So do as follows:
i Guess until the activity is created onorientation change the
bundle is not available, that's way you are not able to retrieve it
in oncreate
Let the activity create in oncreate
Then restore the instance in onRestoreInstanceState
Hope this helps !
hi i have a menu activity that contains 4 buttons when i press button one the second
activity is open and.i added a back button in second activity to come back to activity
one(menu)how would it perform through code can any one help
private ListView lv;
public static ArrayList<String> your_array_list = new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
lv = (ListView) findViewById(R.id.listView1);
}
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
}
#Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, your_array_list );
lv.setAdapter(arrayAdapter);
try {
DisplayM.main();
} catch (Exception e) {
e.printStackTrace();
}
// if (ViewClass.theEnd)
// your_array_list.add(ViewClass.methods);
int lst = 0;
for(int i=0; i<lst; i++)
{
}
}
#Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
}
this is my listview activity
Use below code in your back button
backButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
YourActivity.this.finish();
}
});
Write below code on your activity :-
#Override
public void onBackPressed()
{
// TODO Auto-generated method stub
super.onBackPressed();
}
or
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
or
#Override
public void onBackPressed()
{
// TODO Auto-generated method stub
// if u want to go specific activity then use this code
Intent intent = new Intent(this, YourMainActivity.class);
startActivity(intent);
finish();
}
In your second Activity on your Back Button onClick simply finish() the current (second/third ...) activity. This is what the default back button also does
in pause button you have to just call finish() method and then after finishing current activity it return to parent activity
I wish to remove the ad if they are clicked once.
I implemented the following code.
I also added android:launchMode="singleInstance"
in the manifest and android:alwaysRetainTaskState="true" in the activity section of the manifest.
But when i click on the ad and return to the app the ad is still showing .
My code is as follows.
public class MainActivity extends Activity implements AdListener{
adView = (AdView)findViewById(R.id.ad);
adView.loadAd(new AdRequest());
// my code
#Override
public void onDismissScreen(Ad arg0) {
if (adView != null) {
adView.destroy();
}
// TODO Auto-generated method stub
}
#Override
public void onFailedToReceiveAd(Ad arg0, ErrorCode arg1) {
// TODO Auto-generated method stub
}
#Override
public void onLeaveApplication(Ad arg0) {
// TODO Auto-generated method stub
}
#Override
public void onPresentScreen(Ad arg0) {
// TODO Auto-generated method stub
}
#Override
public void onReceiveAd(Ad arg0) {
// TODO Auto-generated method stub
}
}
adView.setAdListener(this); /* first you should add listener register */
and for dismiss screen
public void onDismissScreen(Ad p1)
{
yourlayout.removeView(adView); // your layout should me remove here
}
friends, I am having trouble during insertion.The insert1.setOnClickListener(new View.OnClickListener() shows Error.Anyone having solution suggest me
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.insert);
ename=(EditText)findViewById(R.id.editText1);
eid=(EditText)findViewById(R.id.editText2);
insert1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
empname=ename.getText().toString();
empid=Integer.parseInt(eid.getText().toString());
Intent insert=new Intent(Insert.this,DBAdapter.class);
Bundle bundle=new Bundle();
bundle.putString("EMPNAME", empname);
bundle.putInt("EMPID", empid);
insert.putExtras(bundle);
startActivityForResult(insert,1);
//DBAdapter db = null;
//db.insert(empname,empid);
}
});
}