mad_chhet
mad_chhet
Let's see the simple example of implicit intent that displays a web page.
1. Single Fragment: Display only one single view on the device screen.
This type of fragment is mostly used for mobile phones. mainActivity.java
2. List Fragment: This Fragment is used to display a list-view from which the
user can select the desired sub-activity. The menu drawer of apps like package example.javatpoint.com.implicitintent
Gmail is the best example of this kind of fragment.
3. Fragment Transaction: This kind of fragments supports the transition from import android.content.Intent;
one fragment to another at run time. Users can switch between multiple
fragments import android.net.Uri;
like switching tabs.
import android.support.v7.app.AppCompatActivity;
Fragment Lifecycle :
Each fragment has it’s own lifecycle but due to the connection with the Activity it
import android.os.Bundle;
belongs to, the fragment lifecycle is influenced by the activity’s lifecycle.
Methods of the Android Fragment import android.view.View;
Description import android.widget.Button;
Methods
import android.widget.EditText;
The very first method to be called when the fragment has been
public class MainActivity extends AppCompatActivity {
associated with the activity. This method executes only once during
onAttach() the lifetime of a fragment.
Button button;
EditText editText;
This method initializes the fragment by adding all the required
onCreate() attributes and components. @Override
button = findViewById(R.id.button);
It indicates that the activity has been created in which the fragment
exists. View hierarchy of the fragment also instantiated before this editText = findViewById(R.id.editText);
onViewCreated() function call.
button.setOnClickListener(new View.OnClickListener() {
The system invokes this method to make the fragment visible on the @Override
onStart() user’s device.
public void onClick(View view) {
String url=editText.getText().toString();
onResume() This method is called to make the visible fragment interactive.
Intent intent=new Intent(Intent.ACTION_VIEW, Uri.parse(url));
It indicates that the user is leaving the fragment. System call this startActivity(intent);
onPause() method to commit the changes made to the fragment.
}
}
System calls this method to clean up all kinds of resources as well as
onDestroyView() view hierarchy associated with the fragment.
;
Android telephony example switch (phoneType) {
. Now open the MainActivity.java file and write the following code there: case (TelephonyManager.PHONE_TYPE_CDMA):
package com.DataFlair.androidtelephony; PhoneType = "CDMA";
import android.app.Activity; break;
import android.content.Context; case (TelephonyManager.PHONE_TYPE_GSM):
import android.os.Bundle; PhoneType = "GSM";
import android.telephony.TelephonyManager; break;
import android.widget.TextView; case (TelephonyManager.PHONE_TYPE_NONE):
public class MainActivity extends Activity { PhoneType = "NONE";
TextView tv; break;
@Override }
protected void onCreate(Bundle savedInstanceState) { // true or false for roaming or not
super.onCreate(savedInstanceState); boolean checkRoaming = tele_man.isNetworkRoaming();
setContentView(R.layout.activity_main); String data = "Your Mobile Details are enlisted below: \n";
tv = findViewById(R.id.textView); data += "\n Network Country ISO is - " + nwcountryISO;
//instance of TelephonyManager data += "\n SIM Country ISO is - " + SIMCountryISO;
TelephonyManager tele_man = (TelephonyManager) data += "\n Network type is - " + PhoneType;
getSystemService(Context.TELEPHONY_SERVICE);
data += "\n Roaming on is - " + checkRoaming;
String nwcountryISO = tele_man.getNetworkCountryIso();
//Now we'll display the information
String SIMCountryISO = tele_man.getSimCountryIso();
tv.setText(data);}}
String PhoneType = ""; // it'll hold the type of phone i.e CDMA / GSM/ None
}}
int phoneType = tele_man.getPhoneType();
public void addRecord(String name, String
CRUD operations (Example. COURSE table (ID, Name, Duration, str += "\n" + list.get(i).id + " " + list.get(i).name + " " +
duration, String description) {
Description),
list.get(i).duration + " " + list.get(i).description;
SQLiteDatabase database =
perform ADD, UPDATE, DELETE and READ operations)
} this.getWritableDatabase();
MainActivity.java
txt.setText(str); ContentValues values = new ContentValues();
package com.subhdroid.lab_j15;
}); values.put("name", name);
//15. Write an android application using SQLite to create table
} values.put("duration", duration);
and perform
} values.put("description", description);
CRUD operations
import android.widget.Button; 7. }
import android.widget.Toast; 9. }
0. Button closeButton;
1. AlertDialog.Builder builder;
2. @Override
4. super.onCreate(savedInstanceState);
5. setContentView(R.layout.activity_main);
8. closeButton.setOnClickListener(new View.OnClickListener() {
9. @Override
1. builder.setMessage(R.string.dialog_message) .setTitle(R.string.dialog_title);
4. .setCancelable(false)
7. finish();
ertbox",
9. Toast.LENGTH_SHORT).show();
0. }
1. })
5. dialog.cancel();
rtbox",
7. Toast.LENGTH_SHORT).show();
8. }
9. });