Showing posts with label Android Studio. Show all posts
Showing posts with label Android Studio. Show all posts

Thursday, 27 November 2014

Android Fragment Back Stack handling

Description

Fragment Back Stack manager while displaying fragments on single activity and need to maintain on back press

Purpose

We know that there is activity stack in Android. We don't need to maintain the stack while opening or closing activity. It will automatically handle the stack and show you the top of activity when you pressed back button.
But in fragment, its neccessary to handle them. Because Android is not going to handle them. We need to create a stack of fragment and manage them while pressing back button.
So, I have created one demo to represent how to handle the fragment in Back Stack.

Usage

In the sample application, you will find one object named fragmentStack. Its a Stack which will push and pop the fragment as per requirement.
Whenever you are displaying any new fragment, just push that fragment into stack using following code.

 //here this fragment is our first fragment  
 homeListFragment = new HomeListFragment();  
 fragmentStack.push(homeListFragment);  

FirstFragment.png

And when you are displaying any other fragment over this fragment, use following code.
We will create a new object of second fragment and add it to stack.

 //here this fragment is second fragment  
 resultListFragment = new ResultListFragment();  
 //hide the last fragment  
 ft.hide(fragmentStack.lastElement());  
 //push the new fragment into stack  
 fragmentStack.push(resultListFragment);  

Second Fragment.png

When backPressed event fires, we will check whether stack size is 2 or not. If it is, then we will pop last fragment and display the previous fragment by following code.

 if (fragmentStack.size() == 2) {  
   FragmentTransaction ft = fragmentManager.beginTransaction();  
   fragmentStack.lastElement().onPause();  
   ft.remove(fragmentStack.pop());  
   fragmentStack.lastElement().onResume();  
   ft.show(fragmentStack.lastElement());  
   ft.commit();  
 } else {  
   //if size is `1` it means first fragment is visible and we can exit from application  
   super.onBackPressed();  
 }  


Click here to download full source code

Friday, 24 May 2013

First Application in Android Studio

<!--[if !mso]> <![endif]
First Application in Android Studio
Android Studio:
Android Studio is a new Android development environment based on IntelliJ IDEA. Similar to Eclipse with the ADT Plugin, Android Studio provides integrated Android developer tools for development and debugging. On top of the capabilities you expect from IntelliJ, Android Studio offers:
  • Gradle-based build support.
  • Android-specific refactoring and quick fixes.
  • Lint tools to catch performance, usability, version compatibility and other problems.
  • ProGuard and app-signing capabilities.
  • Template-based wizards to create common Android designs and components.
  • A rich layout editor that allows you to drag-and-drop UI components, preview layouts on multiple screen configurations, and much more.
Android Studio Installation:
You can download Android Studio from Google’s Developers’ following site.
To get installation guide, visit Getting Started With Android Studio.
Have Execution Problem?

After following all the necessary steps to install Android Studio, you are facing problem to run it? I faced problem and find solution which I wrote at Android Studio not Starting Up After Installation.

First step towards Application

Open Android Studio. Which will display following after initialization.

<![if !vml]><![endif]>

You can see there are 6 tabs there.
<![if !supportLists]>1)      <![endif]>New Project
Use to create a new project which android parameters
<![if !supportLists]>2)      <![endif]>Import Project
Import Android projects from same or other work space.
<![if !supportLists]>3)      <![endif]>Open Project
Open Projects from Directory
<![if !supportLists]>4)      <![endif]>Check out from Version Control
If your Android Studio has configured with Sub-version, you can check-out it.
<![if !supportLists]>5)      <![endif]>Configure
Here you can do settings of plug-ins, import-export settings and many more.
<![if !supportLists]>6)      <![endif]>Docs and How-Tos
You will get help related Studio, tips, key map reference etc.

Step 1: Click on “New Project”
This will open following screen. I have filled it with required information. So do so.
<![if !vml]><![endif]>
When you fill your Application name, package name will be like com.example.anstudapp in your case, which I changed it with com.anstud. You can change package name as per your requirement.
<![if !supportLists]>1)      <![endif]>Application Name
The application name is shown in Play store, as well as Manage Application list in settings.
<![if !supportLists]>2)      <![endif]>Module Name
Module name is used by only Android Studio IDE. Generally module name is same as Application name.
<![if !supportLists]>3)      <![endif]>Package name
Package name is unique identifier of your application. Carefully choose while defining package name for real time application which will uploaded to play store. Play Store will not accept different application with same package name (which is defined in AndroidManifest.xml file).
<![if !supportLists]>4)      <![endif]>Project location
Select your work space where you want to put this project inside.
<![if !supportLists]>5)      <![endif]>Minimum SDK version
Choose lowest version of Android SDK supported by your application.
<![if !supportLists]>6)      <![endif]>Target SDK Version
Choose highest version of Android SDK supported by your application. Target version is that Application is known to work with.
<![if !supportLists]>7)      <![endif]>Compile with
Choose API to compile your Target SDK.
<![if !supportLists]>8)      <![endif]>Theme
Select Base theme of your application.
<![if !supportLists]>9)      <![endif]>Create Custom Launcher Icon
If you like to define custom launcher icon in this wizard, select this option
<![if !supportLists]>10)  <![endif]>Create an Activity
If you like to create activity from default selection, check this option.
<![if !supportLists]>11)  <![endif]>Mark this project as a Library
If you are creating a library, you need to select this option. This project will allow you to refer itself to other project.


<![if !vml]><![endif]>

Step 2: Click Next to Create custom launcher icon
If you have not selected this option in previous screen, you are not able to define custom launcher icon and text in this wizard. Still you can define manually after project creation.

<![if !vml]><![endif]>

You can also select “custom image” from Clipart.
<![if !vml]><![endif]>

You can also select option “Text” to set launcher text icon like following

<![if !vml]><![endif]>

Step 3: Click Next to Define Activity
This screen will let you select default sample activity like Blank Activity, Full Screen Activity, and Master/Detail Activity etc. If you have not selected this option in main screen, you are not able to select this default activity. You need to define new activity in your package yourself.

<![if !vml]><![endif]>
Step 4: Click Next to Create Main Activity
Now, you are at the last step to project creation. Give an Activity name in field, give layout name and if you like to select navigation like “Fixed Tab + Swipe” etc select option from drop down list.
<![if !vml]><![endif]>
Now, click on “Finish” button to complete this project wizard. This will next configure your new project settings. If “Gradle” is not installed in your disk, this will download all the dependencies before this. So wait until set up is finished. If package can’t be downloaded, go to “Configuration” screen, and find “HTTP Proxy” and check whether your internet connection is okay or not.
<![if !vml]><![endif]>
<![if !vml]><![endif]>

Next screen, after successfully configuration, will look like this.

<![if !vml]><![endif]>

Here, you can see that, our “MainActivity” is resided in “src/main/java/com.<package>/”. You can also see our resources structure. The layout file which we defined in “wizard” is here in “src/main/res/layout” directory.

When I open this layout file, following screen will open. I have made some changes in layout file.

<![if !vml]><![endif]>

When you go to “text view” of layout, you are able to view the changes you are making in text file, and output will immediately reflect to your design.

<![if !vml]><![endif]>


This Android Studio has one good facility called “Preview all screen Sizes”, which allow user to see the output of current layout file with all the supported devices simulate output.

<![if !vml]><![endif]>

<![if !vml]><![endif]>

You are almost done with all the things. Now, as you code in Eclipse, same do here. Run your project by selecting AVD or real device.

There are so many features in this IDE, will explore in my next article.

Summary
In this article, we learn how to create a new project using Android Studio’s Project wizard with different parameters. We learn new feature about layout and project structure.
-->