Save Android Activity State Using saveInstanceState

Vrundesha Joshi
Updated on 30-Jul-2019 22:30:25

818 Views

This example demonstrate about How to save an Android Activity state using save instance stateStep 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.     In the above code, we have taken text view to show restore instance elements.Step 3 − Add the following code to src/MainActivity.java import android.os.Build; import android.os.Bundle; import android.os.PersistableBundle; import android.support.annotation.RequiresApi; import android.support.v7.app.AppCompatActivity; import android.widget.TextView; public class MainActivity extends AppCompatActivity {    TextView actionEvent;    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)   ... Read More

Convert LocalDate to java.util.Date by Timezone Offset

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

230 Views

Set the LocalDate to now −LocalDate date = LocalDate.now();Now, set the TimeZone offset −ZoneOffset timeZone = ZoneOffset.UTC;Convert the LocalDate to java.util.Date −Date.from(date.atStartOfDay().toInstant(timeZone))Example Live Demoimport java.time.LocalDate; import java.time.ZoneOffset; import java.util.Date; public class Demo {    public static void main(String[] args) {       LocalDate date = LocalDate.now();       System.out.println("Date = "+date);       ZoneOffset timeZone = ZoneOffset.UTC;       System.out.println(Date.from(date.atStartOfDay().toInstant(timeZone)));    } }OutputDate = 2019-04-19 Fri Apr 19 05:30:00 IST 2019

Delete Record with Lowest ID in MySQL

Daniol Thomas
Updated on 30-Jul-2019 22:30:25

949 Views

To delete record with the lowest id, you can use the following syntax:delete from yourTableName order by yourColumnName limit 1;Let us first create a table:mysql> create table DemoTable (    Id int,    Name varchar(20) ); Query OK, 0 rows affected (0.75 sec)Following is the query to insert records in the table using insert command:mysql> insert into DemoTable values(10, 'Larry'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values(100, 'Mike'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values(30, 'Sam'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(90, 'Chris'); Query ... Read More

HashMap vs WeakHashMap in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

2K+ Views

Details about HashMap and WeakHashMap that help to differentiate them are given as follows −HashMap in JavaA HashMap has key-value pairs i.e. keys that are associated with the values and the keys are in arbitrary order. A HashMap object that is specified as a key is not eligible for garbage collection. This means that the HashMap has dominance over the garbage collector.A program that demonstrates this is given as follows −Example Live Demoimport java.util.*; class A {    public String toString() {       return "A ";    }    public void finalize() {       System.out.println("Finalize method");   ... Read More

SecureRandom getInstance Method in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

399 Views

A SecureRandom object can be obtained using the getInstance() method in class java.security.SecureRandom. This SecureRandom object is useful in implementing the Random Number Generator (RNG) algorithm that is specified.The getInstance() method requires a single parameter i.e. the Random Number Generator (RNG) algorithm and it returns the SecureRandom object.A program that demonstrates this is given as follows −Example Live Demoimport java.security.*; import java.util.*; public class Demo {    public static void main(String[] argv) {       try {          SecureRandom sRandom = SecureRandom.getInstance("SHA1PRNG");          String s = "Apple";          byte[] arrB = ... Read More

Read HTTP Header Using JSP

Ankith Reddy
Updated on 30-Jul-2019 22:30:25

3K+ Views

Following is the example which uses getHeaderNames() method of HttpServletRequest to read the HTTP header information. This method returns an Enumeration that contains the header information associated with the current HTTP request.Once we have an Enumeration, we can loop down the Enumeration in the standard manner. We will use the hasMoreElements() method to determine when to stop and the nextElement() method to get the name of each parameter name.           HTTP Header Request Example                        HTTP Header Request Example         ... Read More

Use Parameterized SQL Query in JSP

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

1K+ Views

The tag used as a nested action for the tag and the tag to supply a value for a value placeholder. If a null value is provided, the value is set to SQL NULL for the placeholder.AttributeThe tag has the following attributes −AttributeDescriptionRequiredDefaultValueValue of the parameter to setNoBodyExampleTo start with the basic concept, let us create an Employees table in the TEST database and create few records in that table as follows −Step 1Open a Command Prompt and change to the installation directory as follows −C:\> C:\>cd Program Files\MySQL\bin C:\Program Files\MySQL\bin>Step 2Login to the database as ... Read More

Collectors averagingLong Method in Java 8

Arjun Thakur
Updated on 30-Jul-2019 22:30:25

382 Views

The averagingLong() method of the Collectors class returns a Collector that produces the arithmetic mean of a long-valued function applied to the input elements.The syntax is as followsstatic Collector averagingLong(ToLongFunction

Update MySQL Column with Integer Based on Order

George John
Updated on 30-Jul-2019 22:30:25

671 Views

The syntax is as follows to update a column with an int based on orderset @yourVariableName=0; update yourTableName set yourColumnName=(@yourVariableName:=@yourVariableName+1) order by yourColumnName ASC;To understand the above syntax, let us create a table. The query to create a table is as followsmysql> create table updateColumnDemo    -> (    -> Id int,    -> OrderCountryName varchar(100),    -> OrderAmount int    -> ); Query OK, 0 rows affected (1.76 sec)Insert some records in the table using insert command.The query is as followsmysql> insert into updateColumnDemo(Id, OrderCountryName) values(10, 'US'); Query OK, 1 row affected (0.46 sec) mysql> insert into updateColumnDemo(Id, OrderCountryName) ... Read More

Remove Uniqueness Constraint from MySQL Table

Samual Sam
Updated on 30-Jul-2019 22:30:25

236 Views

You can use DROP INDEX for this. The syntax is as follows −alter table yourTablename drop index yourUniqueName;To understand the above syntax, let us create a table. The query to create a table is as follows −mysql> create table removeUniquenessConstraint    -> (    -> Id int,    -> Name varchar(100),    -> Age int,    -> isGreaterThan18 bool,    -> UNIQUE(Id, isGreaterThan18)    -> ); Query OK, 0 rows affected (0.69 sec)Now check the details of table with the help of SHOW CREATE command. The query is as follows −mysql> show create table removeUniquenessConstraint;Here is the output −+----------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | ... Read More

Advertisements